Upload other environments or configurations like staging and manufacturing to your iOS app the usage of .xcconfig information in Swift
Should you’re having a look to arrange separate environments/configurations like staging, debug, and unencumber to your iOS app, then you definitely’re in the correct position. Putting in other environments/configurations to your app is in reality necessary and helpful. Earlier than beginning the setup, we must increase an figuring out of why we even want separate environments or configurations in our iOS or undertaking.
Having separate configurations like debug and unencumber to your iOS app is a need; They allow you to in a couple of techniques, together with the next:
- Differentiate your manufacturing API keys
- Create separate API endpoints for separate configurations
- Put separate third-party libraries to your debug construct to make trying out extra seamless. For example, having separate Crashlytics on your debug builds. Word: Crashlytics do supply some way to do that.
- Have separate keys or tokens on your paid libraries. Many of the libraries supply techniques to check freely since you don’t need to check in manufacturing and get billed for that
- Supply debug software. Whilst you’re operating in a big group or small group, it’s just right to have some debug gear like Community Interceptor, trojan horse reporting, and many others., to your trying out builds so trying out can occur in a unbroken means
- Set up. You’ll set up the similar app with separate configurations at the identical tool on the identical time
I’m hoping by way of now we’re at the identical web page that we wish to have separate environments or configurations in our iOS app. Now the query comes of ways we must enforce it. There are numerous techniques to do that, however thankfully Xcode supplies an excessively neat manner to take action the usage of Xcconfig information. So, let’s find out about them and enforce them.
Issues we’ll be protecting on this article are:
- Figuring out about XCConfig information
- Find out how to use them for separate environments and objectives
- Find out how to outline and organize other construct settings the usage of xcconfig
- Find out how to get entry to construct settings values from code
So, what are xcconfig information? xcconfig stands for Xcode Configuration Recordsdata, and so they’re a simple textual content record containing key-value pairs that assist in defining or overriding current construct settings for any undertaking or goal’s construct configuration.
One defines construct configurations on the undertaking stage; that’s why they are able to be used amongst more than a few objectives to your undertaking as neatly.
We will use the Construct Settings pane in Xcode to modify those settings, however that turns into tricky if in case you have a couple of objectives. Additionally, it’s extra susceptible to mistakes, while xcconfig information are a simple method to maintain these kind of issues.
Xcconfig information additionally supply inheritance and feature conditional statements in your construct settings, which is beneficial in sharing settings and customizing. In case you are feeling beaten with this, don’t concern we’ll undergo an instance. It’s in reality easy and simple to make use of.
Sufficient concept about xcconfig information. Let’s make a demo undertaking and get our arms grimy.
I’ve created a brand new app named ConfigTest in Xcode. Be at liberty to code alongside. Should you don’t really feel like coding alongside, obtain the finished code, open the undertaking, and practice alongside.
So, every time one creates a brand new undertaking, Xcode by way of default creates two configurations: Debug and Unlock. You’ll see this within the following screenshot. To view it to your undertaking, choose your undertaking record, click on at the undertaking, and move to the data tab.
Let’s create a brand new configuration by way of clicking at the plus button within the Configurations segment. The next popup must seem. We will reproduction both of the configurations.
We’ll be naming our new configuration “Staging.” It must get started showing the brand new configuration you’ll be able to see underneath. That’s it. That’s the way you create a brand new configuration in Xcode.
Let’s create xcconfig information to present values to our 3 configurations. Press
command + N or Record -> New -> Record. You’ll be brought about with the next window. Make a selection the Configuration Settings record. You’ll additionally seek if you happen to’re now not ready to search out this record.
I’m naming my information in accordance with their configuration names so as to bear in mind them simply. Be at liberty to call them no matter you need.
Word: Remember to don’t have any Goal decided on. As discussed previous, we upload those information on the undertaking stage.
Now, create two extra configuration information for the opposite two configurations. I’ve named my information
Staging.xcconfig. It’s time to place some code into those information.
Debug.xcconfig record and put within the following code:
PRODUCT_BUNDLE_IDENTIFIER = com.anandnigam.ConfigTest.debugBUNDLE_DISPLAY_NAME = ConfigTestDebug
BUNDLE_DISPLAY_NAME belongings will allow you to create other app names for separate configurations, and
PRODUCT_BUNDLE_IDENTIFIER will allow you to run the construct with separate configurations with out deleting the former one out of your telephone or simulator.
Subsequent, upload the next code to your
PRODUCT_BUNDLE_IDENTIFIER = com.anandnigam.ConfigTest.stagingBUNDLE_DISPLAY_NAME = ConfigTestStaging
Upload the next code to your
PRODUCT_BUNDLE_IDENTIFIER = com.anandnigam.ConfigTestBUNDLE_DISPLAY_NAME = ConfigTest
So, now that we’ve got added code in our xcconfig information, we wish to assign our information to their respective configurations earlier than trying out them. Pass for your undertaking’s information tab, amplify the entire configurations, and go your information’ names to their respective configuration as you’ll be able to see underneath. Remember to have assigned all 3 (or extra if in case you have extra configurations).
choice + click on in your app’s title close to the simulator; it’ll open the underneath menu. Or you’ll be able to click on in your app’s title and make a choice Edit Scheme; it’ll open the similar underneath menu. Right here you’ll be able to make a choice no matter configuration you need to run.
So, after working for the entire configurations, you’ll be able to see a couple of apps like the only underneath to your simulator or iPhone. We’re ready to run the app with other names and feature them on one tool on the identical time.
Something to notice, you’ll be able to additionally use the
APP_NAME key and feature other values for it to your config information as a substitute of
BUNDLE_DISPLAY_NAME, however it will reason problems if in case you have check objectives to your current app. I confronted the problem, so for simplicity, I’m best converting the
BUNDLE_DISPLAY_NAME key, however be happy to experiment round.
One necessary tip: this best applies in the event you’re the usage of cocoapods. In a different way, you’ll be able to skip it.
Should you’re going to combine this factor into your current codebase which makes use of cocoapods as a dependency supervisor, you’ll have to accomplish the next steps:
- Delete the
- Run pod set up
- Open new .xcworkspace
- Come with the next line on the best to your respective config information
#come with “Pods/Goal Enhance Recordsdata/Pods-YourAppName/Pods-YourAppName.unencumber.xcconfig”
#come with “Pods/Goal Enhance Recordsdata/Pods-YourAppName/Pods-YourAppName.staging.xcconfig”
#come with “Pods/Goal Enhance Recordsdata/Pods-YourAppName/Pods-YourAppName.debug.xcconfig”
You’ll spot those pod-related xcconfig information at those paths. When you’ve got your pods at any other trail than the standard cocoapods setup, you’ll be able to to find those information beneath the Pods listing and come with their paths.
Transferring on. That is the very elementary factor that we simply did the usage of xcconfig information. Now, let’s make it to the following stage and do a little extra complicated such things as the usage of inheritance, having prerequisites in config information, getting values from config information to your codebase, and likewise tips on how to configure if in case you have a couple of objectives like widget extension or proportion extension on your app
Let’s get started with inheritance and including prerequisites to your config information. For this, let’s make a brand new config record named
Base.xcconfig like we did with the opposite information previous. Open the record and paste the next code:
//MARK: - App SettingsBASE_BUNDLE_IDENTIFIER = com.anandnigam.ConfigTestBUNDLE_DISPLAY_NAME = ConfigTest//MARK: - Conditional ExamsONLY_ACTIVE_ARCH[config=Debug][sdk=*][arch=*] = YES
Right here we’re defining a base package deal identifier so we will inherit this and append issues onto this for our other configurations.
We also are defining the default
Now within the 1/3 line, what we’re doing is environment the flag to construct best energetic structure when the configuration is debugging for any SDK for any structure. * approach any, after we’re operating with the config record.
The generic syntax for the conditional observation is
BUILD_SETTINGS_NAME_KEY[condition=value] = VALUE
Now to peer the inheritance, open
Debug.xcconfig record and substitute the code with the next:
#come with "Base.xcconfig"PRODUCT_BUNDLE_IDENTIFIER = $(BASE_BUNDLE_IDENTIFIER).debugBUNDLE_DISPLAY_NAME = $(inherited)Debug
- We’re inhering the
Base.xcconfigrecord in our
- We’re the usage of the main
BASE_BUNDLE_IDENTIFIER’s price from
Base.xcconfigrecord in our
Debug.xcconfig. if you wish to use any price out of your base config record, you simply wish to outline the main like this
$(KEY_NAME). Then, you’ll be able to get the price and use it
- All through this procedure, we’re overriding the price for a similar key from the
Base.xcconfigrecord by way of inheriting the price and including one thing of ours
So, those are two very important techniques we will inherit, override, or use values from different config information in our config record. Those are useful to reduce the efforts to outline other values and use them
Now, please do the next adjustments within the respective information
Within the Staging.xcconfig record
#come with “Base.xcconfig”//MARK: — App Settings// to make use of any price outlined within the inherited recordPRODUCT_BUNDLE_IDENTIFIER = $(BASE_BUNDLE_IDENTIFIER).staging// to inherit the price from the integrated config recordBUNDLE_DISPLAY_NAME = $(inherited)Staging
Within the Unlock.xcconfig record
#come with "Base.xcconfig"//MARK: - App SettingsPRODUCT_BUNDLE_IDENTIFIER = $(BASE_BUNDLE_IDENTIFIER)
Word: Within the
Unlock.xcconfig record, we’re now not defining the
BUNDLE_DISPLAY_NAME. As an alternative, the discharge configuration will use the key-value from Base.xcconfig record when ran. This manner we will use the price from
Base.xcconfig if we don’t need to customise it.
Now, there’s yet another necessary factor those config information can do this’s useful. So, it’s beautiful glaring one may well be wanting other URLs or API keys for separate configurations. This makes certain you don’t t finally end up hitting manufacturing servers or the usage of production-related API keys whilst debugging or giving trying out builds to QA.
So, let’s outline some API keys and URLs in our config information.
Paste the next code into your respective information:
//MARK: - API KeysFIREBASE_KEY = acnkdjkbvjfbvfibvfj//MARK: - Server URLsBASE_URL = https:/$()/anand2nigam.medium.com
//MARK: - API KeysFIREBASE_KEY = acnkdjkbvjfbvfibvfj//MARK: - Server URLsBASE_URL = https:/$()/www.linkedin.com/in/anand2nigam/
//MARK: - API KeysFIREBASE_KEY = acnkdjkbvjfbvfibvfjscdnkjvrhb//MARK: - Server URLsBASE_URL = https:/$()/github.com/anand2nigam
I’ve deliberately added a random API key and other base URLs to turn how we’ll get those values in each and every of the other config information.
Now, head over for your
Information.plist record and upload the entire keys you’re going to wish to get the values to make use of throughout your codebase. For this situation, we’re going so as to add
Remember to upload the proper title of the main from the config record within the price column in
Information.plist record. The important thing column of
Information.plist will also be populated in accordance with which title one desires to fetch the main. I’m conserving it the similar to keep away from confusion. Right here’s a screenshot to assist:
Via doing the above movements, we now have put the entire values in position we need to fetch. Now, let’s see how we will fetch those values. So, a easy solution can be to fetch the main from
Information.plist and get the price outlined within the config record.
We’re going to do the similar. Let’s make a brand new record; I’m naming it
Config.swift and including the next code:
This record makes an abstraction layer that may assist us in fetching the values (with the proper key title) from the
Information.plist record and use them around the code base. It’s time to check our code and notice if that is even operating. Paste the next code within the view controller record Xcode made for us:
This will likely display the bottom URL price in an alert message to us. Now, run the app in numerous configurations and also you’ll realize we’re ready to make use of the respective base URLs outlined within the configuration information
There’s yet another method to see and investigate cross-check those houses in UI as neatly. Pass for your Goal and Make a selection Construct Settings. Scroll to the ground. You’ll see your whole houses beneath the Person Outlined segment as proven within the screenshot:
Now, let’s duvet the case if in case you have a couple of objectives. On this case, it’s higher to have separate config information for them to keep away from confusion. However one too can use the similar config record by way of defining other keys and the usage of them in numerous objectives. On this article, we’ll be stating separate config information, which is the easiest way to maintain this situation.
So, let’s upload a widget extension for our little ConfigTest app. Pass to Objectives and faucet the plus button on the backside. You’ll be brought about with the next window. Pass forward and title your widget extension. I’ve named mine ConfigTestWidget.
Now, let’s make 3 new config information. I’ve named them
WidgetRelease , and
WidgetDebug. Now, move to Undertaking’s Information tab, amplify the entire configurations, and assign the respective config information to their objectives, as proven within the following screenshot:
This procedure is very similar to what we did previous. I’ve additionally organized the other configuration information of their folders as proven within the above screenshot. Be at liberty to do the similar.
Now, paste the next code into their information:
#come with "ConfigFiles/ConfigTestTarget/Staging.xcconfig"PRODUCT_BUNDLE_IDENTIFIER = $(inherited).ConfigTestWidget
#come with "ConfigFiles/ConfigTestTarget/Debug.xcconfig"PRODUCT_BUNDLE_IDENTIFIER = $(inherited).ConfigTestWidget
#come with "ConfigFiles/ConfigTestTarget/Unlock.xcconfig"PRODUCT_BUNDLE_IDENTIFIER = $(inherited).ConfigTestWidget
As you will have to have spotted, we’re inheriting the price of
PRODUCT_BUNDLE_IDENTIFIER and including our widget title. That is once more an instance of inheriting in xcconfig information and likewise extensions package deal identifiers must be adopted by way of their app’s package deal identifier. So, this fashion we save ourselves the difficulty of repeating the app’s package deal identifier.
Now you’ll be able to run the app and use the widget as neatly for various configurations. I’ve added some code within the widget to save lots of time. Be at liberty to take it from GitHub. It must glance one thing like the next screenshot:
Now you must have the ability to run the 3 configurations having other app names and different houses with out deleting any app from the tool. We will additionally give separate app icons to our other configurations. Simply upload some other app icon set in
Property.xcassets with a reputation like AppIcon-Debug and outline the next key to your config information:
In Debug.xcconfig and in Staging.xcconfig
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon-Debug
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon
If your adjustments don’t replicate, move for your Goal’s construct settings and seek for this key Number one App Set Icon. Double-tap and edit the sector with
$(inherited). This will likely get started taking values out of your xcconfig information, and it’s going to glance one thing like the next screenshot:
That’s it. This covers many of the stuff about xcconfig information that one would require to enforce in a brand new or current undertaking. On this educational we coated the next:
- Find out how to configure your app for a couple of environments/configurations like Staging, Debug, and Unlock
- Find out how to use xcconfig information to take action
- Find out how to proportion values amongst other config information
- Find out how to retrieve values from config values
With these kind of issues associated with config information, you’re just right to head and configure your tasks. Be at liberty to seem up the entire above code from GitHub and use it for reference.
Those hyperlinks will allow you to perceive extra about different houses and issues in xcconfig information. Thank you for studying.
Wish to Attach?You'll to find me on LinkedIn.