2

How to distinguish multiple environments in iOS app?

My Swift app uses external API connection and we have two API URLs - testing and productive.

Is there any way how to use one variable and set its value for testing in Xcode and another value for the AppStore release?

Currently I am using Xcode 6.3.1 Application deployment target is iOS 8.1

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Matej
  • 43
  • 1
  • 7
  • See http://stackoverflow.com/questions/24645090/how-to-access-build-settings-in-xcode-6 for a possible solution using user-defined settings. – Martin R May 15 '15 at 09:45

1 Answers1

3

In swift we need to use "Swift Compiler - Custom Flags" instead of Pre Processor macro...

Set flag as shown in figure below enter image description here

Use below code for checking..

    var url;

    #if DebugURL
        url = ** Your Debug URL **
    #else
        url = ** Your Release URL **
    #endif

Note : You add the DEBUG symbol with the -D DEBUG entry.

Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40
  • I see I can use this preprocessor macro value in the code, so would it be a good solution to use the same named macro with different values for DEBUG and RELEASE? Something like DEBUG: API_URL = 1234, RELEASE: API_URL = 456. And then in the code I would just use url=API_URL...??? – Matej May 15 '15 at 09:45
  • @ThomasKilian, Thank for your comment.. Just updated my answer.. please remove your down vote if you think it's proper now.. – Nilesh Patel May 15 '15 at 10:08
  • Thank You for the update Nilesh. I just tried it in the simulator and on the physical device and I always fell in the IF branch. Now I hope it will work in the released application too. I will also test it on the TestFlight because I am really excited if I will receive the DebugURL or ReleaseURL value...Edit: If I understand it depends on archive setup in scheme settings, or? – Matej May 15 '15 at 11:08