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
dev4life
  • 10,785
  • 6
  • 60
  • 73
  • 4
    Are you sure that's what you actually need? Maybe several configurations (debug, release, production) can solve your problem? – dreamzor Jan 20 '17 at 15:07
  • You can use http://stackoverflow.com/questions/4744826/detecting-if-ios-app-is-run-in-debugger to detect if it's being debugged (not totally what you want, since you can launch it but disable the debugging from XCode). – Larme Jan 20 '17 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 Jan 20 '17 at 18:00

2 Answers2

8

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.

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
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
Rhm Akbari
  • 362
  • 3
  • 12