Yi Jiang
Yi Jiang

Reputation: 4028

Which one is the best practice for multiple environment settings in Xcode for iOS project?

Till now, I know there a couple of solutions for multiple environment settings.

Solution 1: Prepare different plist files for the different environment. Then, use Add Run Script Build Phase to copy the specific plist file to overwrites project plist file.

Solution 2: In PROJECT -> Info -> Configurations, duplicate a existing configuration. setup scheme -> Info -> Build Configuration. Then, in the targets -> Build settings, add user-defined settings.

$(kBaseURL)

Solution 3: Add preprocessor macros in PROJECT -> Build Settings to define the environment. Then, in source code write the macro to switch environment setting value.

#ifdef DEBUG_ENV
#define kBaseURL @"http://debug-server.a.com/api/"
#else
#define kBaseURL @"http://production-server.a.com/api/"
#endif

There are several other ways to switch environments settings.

Which way is the best practice to do this?

Upvotes: 3

Views: 784

Answers (1)

JanMensch
JanMensch

Reputation: 2435

I think this is also a question of taste and of what you are actually trying to achieve.

For example you might want to have a debug configuration that does not talk to the internet and rather uses HTTP stubs to mock the requests. This won't be possible by simply copying plists.

Personally I prefer creating build configurations for each environment, then set a compiler flag for each config that switches precompiler macros. I also like to take some more tricky steps to modify the bundle id and app name for all configs and then create a build scheme for every config. This results in me being able to build all environments from Xcode and install all apps on the same device, as the bundle ids are different. It's a bit tricky to set up though. However once it runs: perfect for my use cases :)

Upvotes: 1

Related Questions