2

I have to make sure, that the app is not sending device token for push notifications, if it build in debug, and send if in release.

Can I use this code?

#if RELEASE
[SendTokenRequest sendDeviceToken:deviceToken withCompletion:nil];
#endif

Obviously it's hard to test, it compiles and not executing in debug, just wan't to make sure it will be executed on production...

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157

3 Answers3

5

Yes, it should work in your production case, assuming RELEASE is preprocessor directive created only in the release build step.

I do a similar thing in the app I work on, but using a a DEBUG macro instead (inverting your test). I also add #else to ensure it is logged in debug.

#ifndef DEBUG
// Make sure this is executed in debug
NSLog(@"Send token in production");
#else
// Release code
#endif
thegrinner
  • 11,546
  • 5
  • 41
  • 64
  • I think your example would be: ` #ifndef DEBUG // Release code NSLog(@"Send token in production"); #else // Debug code #endif ` Or use #ifdef DEBUG if you want the #else to execute Release code – Guilherme D Heynemann Bruzzi Feb 08 '17 at 18:11
3

You can use a flag like you want using this following method to test if you are in debug, adhoc, release or debug mode :

enter image description here

Go to your Build Settings and add other flags like the previous picture.

Next you can use that in your project (in your case) :

#ifdef __RELEASE__
// Do what you want
#endif

you can use __DISTRIBUTION__ __RELEASE__ __DEBUG__ __AD_HOC__ depending your scheme settings

enter image description here

Jordan Montel
  • 8,227
  • 2
  • 35
  • 40
0

Old question, but I found another approach. Add this pair to Info.plist

<key>Configuration</key>
<string>${CONFIGURATION}</string>

Now you are able to check if app is release or debug:

var config = Bundle.main.infoDictionary?["Configuration"]
Ogulcan Orhan
  • 5,170
  • 6
  • 33
  • 49