128

In Objective-C it was sometimes useful to use static string constants to define alternate API keys (for example to differentiate between RELEASE and DEBUG keys for analytics packages, like MixPanel, Flurry or Crashlytics):

#if DEBUG
static NSString *const API_KEY = @"KEY_A";
#else
static NSString *const API_KEY = @"KEY_B";
#endif

and then...

[Analytics startSession:API_KEY];

How does this translate to Swift, since the Swift compiler no longer uses a preprocessor?

mfaani
  • 33,269
  • 19
  • 164
  • 293
cleverbit
  • 5,514
  • 5
  • 28
  • 38

4 Answers4

207

Apple included full support for Swift preprocessor flags as of Xcode 8, so it's no longer necessary to set these values in "Other Swift Flags".

The new setting is called "Active Compilation Conditions", which provides top-level support for the Swift equivalent of preprocessor flags. You use it in exactly the same way as you would "Other Swift Flags", except there's no need to prepend the value with a "-D" (so it's just a little cleaner).

From the Xcode 8 release notes:

Active Compilation Conditions is a new build setting for passing conditional compilation flags to the Swift compiler. Each element of the value of this setting passes to swiftc prefixed with -D, in the same way that elements of Preprocessor Macros pass to clang with the same prefix. (22457329)

enter image description here

You use the above setting like so:

#if DEBUG
    let accessToken = "DebugAccessToken"
#else
    let accessToken = "ProductionAccessToken"
#endif
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
  • 6
    Note: you should **not** specify =1 or any other = value. Rather, you need to just specify the flag name. :] – JRG-Developer Jan 11 '19 at 22:44
  • @JRG-Developer I don't disagree, but I'm not sure how your comment applies here. – Dan Loewenherz Jan 13 '19 at 23:54
  • 12
    This is a helpful answer, but coming from an Objective-C background (as I imagine many iOS developers are), I assumed that I needed to specify `=1`... I lost a bit of time trying to figure out why it wasn't working when I did. So, I thought I'd share this tidbit to help the next fellow. :] Anyways, thanks for your answer here! – JRG-Developer Jan 16 '19 at 20:43
  • 3
    @JRG-Developer, @Dan Loewenherz I have set both `DEBUG` in `Active Compilation Conditions` and `DEBUG=1` in `Preprocessor Macros` and this configuration does not work at all. Should i remove `DEBUG=1` ?? Not clear from the above comments. – Bhavin_m Apr 26 '19 at 06:48
  • @Bhavin_m The only things I can think of are 1) you're building under another release configuration or 2) the compilation conditions are being overriden later on in the resolution chain. – Dan Loewenherz Apr 26 '19 at 15:53
  • 2
    @DanLoewenherz You are absolutely right. I had set "DEBUG" for archive configuration in my target settings, so every time it runs a Debug statement and never runs the release condition. Anyone who is facing issue then please check you target's `Build Configuration` first. Check this answer https://stackoverflow.com/questions/9063100/xcode-ios-how-to-determine-whether-code-is-running-in-debug-release-build/38987783#38987783 for more info. – Bhavin_m Apr 29 '19 at 07:17
136

UPDATED: Xcode 8 now supports this automatically, see @dwlz's response above.

Prior to Xcode 8, you could still use Macros in the same way:

#if DEBUG
let apiKey = "KEY_A"
#else
let apiKey = "KEY_B"
#endif

However in order for them to be picked up by Swift, you need to set "Other Swift Flags" in your target's Build Settings:

  • Open Build Settings for your target
  • Search for "other swift flags"
  • Add the macros you wish to use, preceded by the -D flag

enter image description here

Gobe
  • 2,559
  • 1
  • 25
  • 24
cleverbit
  • 5,514
  • 5
  • 28
  • 38
11

In swift packages you have to do this inside of the swiftSettings argument to .target in your Package.swift file. Use the define method (Apple documentation) or Swift documentation

targets: [
.target(name: String,
            dependencies: [Target.Dependency],
            path: String?,
            exclude: [String]?,
            sources: [String]?,,
            cSettings: [CSetting]?,
            cxxSettings: [CXXSetting]?,
            swiftSettings: [SwiftSetting]?,
            linkerSettings: [LinkerSetting]?),

Mine looks like this and it works!

            swiftSettings: [
               .define("VAPOR")
            ]

in my code I can conditionally compile using this:

#if VAPOR
garafajon
  • 1,278
  • 13
  • 15
6

As a follow up observation, try not to keep api keys / secrets in plaintext in the repository. Use a secrets management system to load the keys / secrets into the user's environment variables. Otherwise step 1 is necessary, if acceptable.

  1. Put the "secrets" in a plaintext file above in the enclosing repository
  2. Create a ../set_keys.sh that contains a list of export API_KEY_A='<plaintext_key_aef94c5l6>' (use single quote to prevent evaluation)
  3. Add a Run script phase that can source ../set_keys.sh and move it to the top of execution order
  4. In Build Settings > Preprocessor Macros, add to defines as necessary such as API_KEY_A="$API_KEY_A"

That captures the environment variable into the compiler define which is later used in each clang invocation for each source file.

Example directory structure

[10:33:15] ~/code/memo yes? tree -L 2 .
.
├── Memo
│   ├── Memo
│   ├── Memo.xcodeproj
│   ├── Memo.xcworkspace
│   ├── Podfile
│   ├── Podfile.lock
│   └── Pods
└── keys
Michael Lorenzo
  • 628
  • 10
  • 20