Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do conditional compile based on target names?

Tags:

xcode

ios

I'm using testflightapp.com SDK in my project to track crashes during beta test which is conducted on testflightapp.com. I have 2 targets in my Xcode project, which are "ad-hoc" and "app store".

To initiate testflight SDK, I need to put down a line of code in the AppDelegate.m like:

[TestFlight takeOff:@"67bebb8d8e5396a...A4LjQwNjQ4NA"];

Now, I want the compiler to exclude this line of code when I compile for the target "app store", because it doesn't make sense to trigger testflight SDK when it goes to public.

I'm expecting something like #ifdef, but I couldn't get a clue by searching the forum.

like image 637
Chris Chen Avatar asked Feb 27 '12 03:02

Chris Chen


2 Answers

You can use a user-defined build setting that you set in the build settings for each target, and then use an #if or #ifdef directive to test that setting. For example, select your 'ad-hoc' target, click on 'Build Settings', and scroll down to the 'User-Defined' section. Then just click the 'Add Build Setting' button and choose 'Add User-Defined Setting'. You can set different values for your setting for each build configuration (debug, release, etc.).

Picture of relevant Xcode settings

like image 82
Caleb Avatar answered Nov 13 '22 09:11

Caleb


It is a old post but would like to share another way to achieve this.

The solution is like create few (depends on how much targets you have) files with same class/function/variable but added to different targets.

Like for me, I have main target and UITest target so I have following two files

BuildConfiguration_main.swift added to main target.

struct BuildConfiguration {
    static let isMainTarget = true
    static let isUITestTarget = false
}

BuildConfiguration_UITest.swift added to UITest target.

struct BuildConfiguration {
    static let isMainTarget = false
    static let isUITestTarget = true
}

Then in you code, you can use BuildConfiguration.isMainTarget or BuildConfiguration.isUITestTarget to tell where is target the code is running if you share some codes between two targets.

like image 6
River2202 Avatar answered Nov 13 '22 10:11

River2202