Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - detect if app is running from Xcode [duplicate]

I'm trying to enable/disable parts of my code based on whether or not the code be being run via USB/Xcode (debug), or in production mode downloaded from the app store (release). I'm aware of checking if it is running in DEBUG or RELEASE mode like this:

enter image description here

#ifdef DEBUG

// Stuff for debug mode

#else

// Stuff for release mode

#endif

but the problem is that an obvious loop-hole I see is you can change the Build Configuration for the 'Run' build scheme from 'Debug' to 'Release'. A better way would be if I can simply detect if it is running from Xcode or not. I haven't found a way to check for this.

Is there a way to check if an iOS app is running from Xcode or not?

like image 487
Kevin_TA Avatar asked Dec 02 '14 15:12

Kevin_TA


People also ask

How to run iOS apps on Xcode?

You need to plug the iPhone into the Mac that’s running Xcode. Then you must download the app to your Mac. When you select a real device in Xcode for testing, you need to add your Apple ID in the Accounts preferences of your project editor. If you’re part of the Apple Developer Program, you must register the device before running the app.

How do I debug an app in Xcode?

Click the Run button to build and run the app on the selected simulated or real device. View the status of the build in the activity area of the toolbar. If the build is successful, Xcode runs the app and opens a debugging session in the debug area.

How do I add an Apple ID to my Xcode project?

When you select a real device in Xcode for testing, you need to add your Apple ID in the Accounts preferences of your project editor. If you’re part of the Apple Developer Program, you must register the device before running the app. You can also pair Xcode with iOS and tvOS apps if that device is on the same network as Xcode.

How do I test my iOS app on a real iPhone?

If you’re using Xcode to create an iOS app, you can test your app using a real iPhone and iOS device with Xcode’s built-in simulator. While simulators are great, some features don’t quite work the same unless you’re using an iPhone. For example, maybe you want to send an SMS message from your iOS application.


1 Answers

You can check if a debugger is attached (probably, but not definitely, Xcode) using sysctl. Here's how HockeyApp does it:

#include <Foundation/Foundation.h>
#include <sys/sysctl.h>

/**
 * Check if the debugger is attached
 *
 * Taken from https://github.com/plausiblelabs/plcrashreporter/blob/2dd862ce049e6f43feb355308dfc710f3af54c4d/Source/Crash%20Demo/main.m#L96
 *
 * @return `YES` if the debugger is attached to the current process, `NO` otherwise
 */
- (BOOL)isDebuggerAttached {
  static BOOL debuggerIsAttached = NO;

  static dispatch_once_t debuggerPredicate;
  dispatch_once(&debuggerPredicate, ^{
    struct kinfo_proc info;
    size_t info_size = sizeof(info);
    int name[4];

    name[0] = CTL_KERN;
    name[1] = KERN_PROC;
    name[2] = KERN_PROC_PID;
    name[3] = getpid(); // from unistd.h, included by Foundation

    if (sysctl(name, 4, &info, &info_size, NULL, 0) == -1) {
      NSLog(@"[HockeySDK] ERROR: Checking for a running debugger via sysctl() failed: %s", strerror(errno));
      debuggerIsAttached = false;
    }

    if (!debuggerIsAttached && (info.kp_proc.p_flag & P_TRACED) != 0)
      debuggerIsAttached = true;
  });

  return debuggerIsAttached;
}
like image 172
Steven Fisher Avatar answered Oct 23 '22 10:10

Steven Fisher