2

I'm developing an iOS app with Swift 2 and, like most developers, I have a staging and a production environment, with different servers, URL and settings.

I'm looking for a way to quickly switch between the two configurations as I'm developing the app.

On Android I could use build types and flavors to solve this problem.

I've read a lot of guides around the web but all of them were in Obj-C and relied on the macro preprocessor and the #ifdef that was available in Obj-C but isn't in Swift.

There are no clear guides on how to do this in swift and, being a total beginner, I'm not even sure where to start looking.

To recap: what I'm looking for it's a way to switch between two configurations (ex 2 property list files) and to reference the settings contained in those configurations from my code, based on the build I've selected.

Jack
  • 171
  • 2
  • 16

1 Answers1

2

You can add a user defined setting in Target's settings with different values for each scheme (Debug, Release, Ad-Hoc, AppStore etc) and use the user defined variable in info.plist file (or as you call it configurations).

Check answer to this question. Although the question is specific to Facebook App Id setup, the answer applies to any generic setting.

Once you have correct data in info.plist you can directly use it in code.

Community
  • 1
  • 1
Swapnil Luktuke
  • 10,385
  • 2
  • 35
  • 58
  • That is exactly what I was looking for. I didn't know I had to also modify the info.plist file in order to make the user settings work. Thanks a lot – Jack Sep 08 '15 at 09:16
  • Another way of accessing the user defined build settings could be pre-processor macros [as suggested in these answers](http://stackoverflow.com/questions/3968919/how-can-i-access-a-user-defined-xcode-build-setting). But getting them through info.plist is much easier and looks relatively cleaner. – Swapnil Luktuke Sep 08 '15 at 09:53