How to set up independent runtime parameter sets in Xcode

My iPhone application connects to three different servers, they say: production, production and testing. There are many configuration values ​​that the application uses depending on which server it connects to, for example, the Facebook application identifier, the TestFlight command key, etc.

I would like to have all the settings in GIT and select only the configuration that the application should use when compiling or releasing. For example, when testing is selected, Product → Run in Xcode launches the debug version of the application connected to the test , and Product → Archive creates an IPA file with the release version, which also connects to the test .

I don't want to create more build configurations than debugging and release (because that would mean 6 different combinations of build configurations / runtime configurations). The ideal solution, as I understand it, would be that I have three schemes: production, testing and production, and each scheme selects one of three Info.plist files for use with the application. This would allow me not only to define various runtime parameters, but also different versions of applications or package identifiers depending on the internal server. But it seems that I can’t configure the “Archive” action in any other way than choosing a different assembly configuration. Any ideas if this can be achieved in any way?

Edit: to make it a little clearer, production / preparation / testing is a server, not an iOS version of an application. The iOS application is available in two versions: debugging / release . In other words, I might want to run a debug version of an application connected to a production server, for example, to debug a failure caused by the return of JSON from that server. I could name the servers as A, B and C for clarity.

+57
ios iphone xcode runtime configuration
May 08 '12 at 11:17
source share
4 answers

I would suggest using different build goals for each environment. I have successfully used this model before. In the settings of your project, you can duplicate the current goal and change the build settings as necessary. There is an Info.plist File property that allows you to change the default value for this purpose.

After that, you can create a diagram for each environment that will use the corresponding object.

You can go one step further and use a different package identifier for each target and different names. This will allow you to install both intermediate and production assemblies on one device, for example.

The only drawback to this is that you have more work when you want to update training profiles.

+10
May 8 '12 at 11:28
source share

A good way to do this would be with assembly configurations and C macros. This avoids the need to create a separate goal for each configuration, which is not really the right use of goals.

First you want to configure configurations at the project level:

enter image description here

You can create various configurations for debugging, corporate distribution, and any other type of custom build you want.

Next, you can define several macros for each configuration that will be passed to the compiler. You can then check these flags at compile time. Locate the "Preprocessor flags" assembly setting at the target level:

enter image description here

If you expand the triangle, you can define different values ​​for each of your configurations. Here you can define macros KEY=VALUE or just KEY .

enter image description here

In your code, you can check for the presence of these macros or their value (if any). For example:

 #ifdef DISABLE_FEATURE_X featureXButton.hidden = YES; #endif // ... #if FOOBAR_VISIBLE == 0 foobarView.hidden = YES; #elif FOOBAR_VISIBLE == 1 foorbarView.hidden = NO; #else #error Invalid value for FOOBAR_VISIBLE #endif 

You can also pass string values ​​that should be wrapped in single quotes in the assembly setup, for example. DEFAULT_LOCALIZATION_NAME='@"en"' .

You can also configure which configuration is used during Debug and Archive time using the Schemes editor. If you select Run or Archive in the schematic editor, you can select the appropriate configuration.

enter image description here

If you need to parameterize the entries in the Info.plist file, you can determine their value using the custom assembly setting. Add custom build customization for your purpose:

enter image description here

And then give it the appropriate value for the different configurations:

enter image description here

Then in the Info.plist file you can refer to this parameter:

enter image description here

Note that one limitation of this approach is that you cannot change the following elements:

  • Settings.bundle

In addition, in older versions of Xcode without directory support, you cannot change the following items:

  • Icon.png
  • Default.png

They cannot be explicitly defined in the Info.plist file or elsewhere, which means you need different goals to change them.

Hope this helps.

+112
May 8 '12 at 12:09
source share

It is much simpler here if the corresponding libraries allow you to set keys in the code, which means that you can have production value in your plist file, but change them in your AppDelegate (or in any of the files in which they are used first).

Works with facebook, twitter and google sdk at the moment.

Example:

 #ifdef DEBUG // Facebook [FBSettings setDefaultAppID:@"SandboxID"]; // Fabric / TwitterKit - must be called above [Fabric with:@[TwitterKit]]; [[Twitter sharedInstance] startWithConsumerKey:@"SandboxKey" consumerSecret:@"SandboxIDSecret"]; #endif 

Same thing in Swift, just use #if instead of #ifdef.

Note about Facebook . This worked with version 3 of their SDK, I'm not sure if this is possible with later versions.

+2
Apr 17 '15 at 6:42
source share

This is probably a very low efficient technology, but I just have an apiURL() method that returns the API URL that I want. I have localhost, stage and production, and I just uncomment the one I want. So far, this has worked well for me. I just forgot to switch it back several times. Unfortunately.

0
Feb 27 '18 at 18:34
source share



All Articles