Brian Marsh
Brian Marsh

Reputation: 617

Environment variables in different schemes for use in code Xcode Swift

My project, with one target, has three configurations:

Debug, UAT, Release

I have four shared schemes that use these configurations for build/run/test/profile/analyse/archive.

In my app I have different bundle IDs for each scheme. This was easy to change, I did so within Product Bundle Identifier for each configuration.

However, I want each scheme to connect to a different REST API. (dev.example.com/api etc)

What is the best way of setting a variable for each environment and then using it within my app?

I have looked at:

1) Swift Compiler>Custom Flags (Mine are currently $(inherited) "-D" "COCOAPODS")

2) NSProcessInfo.processInfo().environment

3) Adding a Configuration.plist file for each environment

Basically, it's not clear to me which is the best way of doing this.

Thank you for your help.

Upvotes: 2

Views: 7876

Answers (2)

aimak
aimak

Reputation: 540

I usually prefer this :

I define constants in different .xcconfig files, then I use them for keys in one single plist file.

Let's say you'd have this in your debug.xcconfig :

<pre>
BASEURL = api.dev.com/api/"
</pre> 

Then, in your plist, you'd add a key baseUrl = http://${BASEURL}

Then, in code, you'd access it with NSBundle.mainBundle().infoDictionary?["baseUrl"]

This is a great article if you want to know more about xcconfig : http://www.jontolof.com/cocoa/using-xcconfig-files-for-you-xcode-project/

Upvotes: 3

tskulbru
tskulbru

Reputation: 2346

Personally I use different .plist files for each scheme. I tried the custom flags approach at first, but as the app grew i needed more and more scheme-based configurations and things got messy.

Different .plist files worked wonders for my project at least.

Upvotes: 2

Related Questions