2

I would like to be able to switch certain functionality on/off in an iOS app depending on whether it is the release version or not. Is it possible to programmatically whether the current build is the release version or not? I know similar functionality can be achieved through the use of macros, but as I understand it these would not work if the code in question is inside a static library.

Ellis
  • 689
  • 1
  • 11
  • 24
  • 5
    `#ifdef DEBUG` should do the trick. –  Oct 11 '13 at 11:02
  • The DEBUG macro is defined at Target > Build settings > Preprocessor Macros. There will be two entries: Debug and Release. The default is to have a macro DEBUG=1 defined in the Debug entry. – Jano Oct 11 '13 at 11:14

3 Answers3

2

create a flag in your scheme and use it like

#ifdef BETA

so say you want to have a string method returning two different strings for two different states

- (NSString *)someString {

  #ifdef BETA
    return @"Beta String";
  #else
    return @"Release String";
  #endif

}

You could use the built in

#ifdef DEBUG

this would differentiate between release and debug

I wouldnt recommend having two different targets.

mooncitizen
  • 464
  • 5
  • 10
  • Will this approach still work if I want to supply a static library to another third party? I want to be able to encapsulate the code that turns off functionality if the build is a release build, without the user of the library having to configure this or having any knowledge of it at all. – Ellis Oct 11 '13 at 11:47
  • it should do if you use the prerequisite DEBUG flag otherwise you would have to make it a stipulation of the framework – mooncitizen Oct 11 '13 at 12:44
0

As H2CO3 said:

#ifdef DEBUG
    NSLog(@"Debug mode");
#endif
ader
  • 5,403
  • 1
  • 21
  • 26
-1

Just set Target name in your Scheme -> Environment Variables -> add Name and Value. eg: targetName = "mytesttarget"

Obj-c

NSDictionary* envir = [[NSProcessInfo processInfo] environment];  
NSString* targetName = envir[@"targetName"];

Swift

let envir = NSProcessInfo.processInfo().environment  
let targetName = envir["targetName"]

Now you can check the target condition

if targetName == "mytesttarget" {  
...  
} else {  
...  
}

Swift 4:

let envir = ProcessInfo.processInfo.processName  
let targetName = envir["targetName"]
channu
  • 223
  • 3
  • 10
SaRaVaNaN DM
  • 4,390
  • 4
  • 22
  • 30