Swift: How to Use Preprocessor Flags (Like '#If Debug') to Implement API Keys

Swift: how to use PREPROCESSOR Flags (like `#if DEBUG`) to implement API keys?

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)

Sample Image

You use the above setting like so:

#if DEBUG
let accessToken = "DebugAccessToken"
#else
let accessToken = "ProductionAccessToken"
#endif

Conditional compiling in Swift

You can use like this :

class ViewController: UIViewController {
// Your common functions
}
#if UseAds
extension ViewController: XYZBannerDelegateProtocol {
// Your delegate methods
}
#endif

How to jump controller through debugging

yes you can use,

  1. place a breakpoint at let vc = ViewController(nibName: "ViewController",bundle: nil)//here breakpoint set
  2. click the "Next" button for debug and in debug window paste this
    line using po command and press enter.
    po navigationController?.pushViewController(vc, animated: true)

and now press the play button of debugging..it will execute your code.

How to determine whether code is running in DEBUG / RELEASE build?

Check your project's build settings under 'Apple LLVM - Preprocessing', 'Preprocessor Macros' for debug to ensure that DEBUG is being set - do this by selecting the project and clicking on the build settings tab. Search for DEBUG and look to see if indeed DEBUG is being set.

Pay attention though. You may see DEBUG changed to another variable name such as DEBUG_MODE.

Build Settings tab of my project settings

then conditionally code for DEBUG in your source files

#ifdef DEBUG

// Something to log your sensitive data here

#else

//

#endif

How to define a preprocessor symbol in Xcode

Go to your Target or Project settings, click the Gear icon at the bottom left, and select "Add User-Defined Setting". The new setting name should be GCC_PREPROCESSOR_DEFINITIONS, and you can type your definitions in the right-hand field.

Per Steph's comments, the full syntax is:

constant_1=VALUE constant_2=VALUE

Note that you don't need the '='s if you just want to #define a symbol, rather than giving it a value (for #ifdef statements)

Is there a preprocessor flag for Ad-Hoc builds?

The answer to this question goes into depth about how to create custom configurations and flags. Its not perfect but it works.

Still looking for an option that is more permanent and cross project compatible without having to spend time at the beginning of each project to set this up.

iOS, detect ad-hoc from code



Related Topics



Leave a reply



Submit