Including a number of targets to swift undertaking is a necessity of hour. For instance, in case your app level to completely different finish factors. One finish level is pointing to improvement server and second endpoint is for manufacturing or dwell server, then most typical strategy for a newbie iOS developer is to remark out completely different finish factors whereas producing builds for particular surroundings. Creating particular person targets for various configurations saves iOS builders from this headache of commenting and uncommenting code for various configurations/settings.
Why we have to add a number of targets in swift
- If our app is utilizing completely different servers finish factors
- Single app goes for use for various customers(instance buyer person and admin person)
- Have to made two app model (Free and Paid)
Steps so as to add a number of targets in swift
1. Create a reproduction goal as proven in picture
2.Rename this duplicate goal. We renamed it to Manufacturing. Similar change title of information.plist file created by xcode for our manufacturing goal.
3.Point out identical title in construct setting of Manufacturing goal.
4. Add swift flag for our targets as proven in under picture. Flags are required as a way to place checks programmatically. Do these steps for all targets and simply substitute identifier. On this picture we used “Dev”. For Manufacturing goal we’ll use “Prod”.
5. Go to energetic schemes, subsequent to play and cease button. Attempt to choose a scheme and click on on new scheme. A pop up will seem. Choose Manufacturing goal and title scheme as identical title i.e. Manufacturing.
Programmatically including checks for various targets
Open ViewController.swift and add under code
#if Dev print("We're utilizing dev model") #else print("We're utilizing manufacturing goal") #endif
Right here we use the flags set by us in construct setting of the goal. For default goal we set flag as “-D Dev”. Thus right here compiler will test for swift flags for targets. If in case you have greater than two targets then you’ll be able to as
#if Dev print("We're utilizing dev model") #elseif Staging print("We're utilizing staging model") #else print("We're utilizing manufacturing goal") #endif
The place to go from right here
On this put up, we discovered that how we are able to add a number of targets to swift undertaking. By including a number of targets developer cab be relieved from ache of commenting uncommenting codes associated to completely different app variations.