4

how to detect if my app was launched from xcode after compilation or from package bundle downloaded from itunes?

The code below does not seem to work, given that the else block always gets executed when I build and run it from xcode.

#if (TARGET_OS_SIMULATOR)

#else
    //Xcode did not launch this app
#endif
3
  • 4
    Are you sure that's what you actually need? Maybe several configurations (debug, release, production) can solve your problem?
    – dreamzor
    Commented Jan 20, 2017 at 15:07
  • You can use stackoverflow.com/questions/4744826/… to detect if it's being debugged (not totally what you want, since you can launch it but disable the debugging from XCode).
    – Larme
    Commented Jan 20, 2017 at 15:28
  • Your TARGET_OS_SIMULATOR test is just testing if this was built for the simulator. It has nothing to do with how it's launched. Generally speaking, the right answer here is a build configuration, not a "I was launched from Xcode" test.
    – Rob Napier
    Commented Jan 20, 2017 at 18:00

2 Answers 2

9

I dont see a WIDE use for this but it is possible by looking at the environment variables. namely is OS_ACTIVITY_DT_MODE" = YES when started via xcode

EXAMPLE:

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
UITextView *v = self.view.subviews.firstObject;

if([environment[@"OS_ACTIVITY_DT_MODE"] boolValue]) {
    v.text = @"xcode attached";
}
else {
    v.text = @"not xcode";
}

NOTE:

the parameter in env is private and may change but the env will likely always be a good place to check this.

2
  • can we rely on this for both: ios and osx targets?
    – dev4life
    Commented Jan 20, 2017 at 16:18
  • 3
    Now you've been given a start point, maybe try it out and see? Commented Jan 20, 2017 at 16:21
2

This is what I think is the best solution I used it in many apps. First set a DEBUG variable in your project's 'Build Setting' in the section shown in the picture. enter image description here

Then use it in your code this way. The code in the #ifdef branch does not even get compiled when the app is being built for release or distribution.

#ifdef DEBUG
    // in debug mode when running off of XCode in debug mode
#else
    // running off of XCode in release mode or downloaded from App Store
#endif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.