Andre Cytryn
Andre Cytryn

Reputation: 2705

Is there a way to recognize if I'm running app from XCode

For example, I would like to connect to my Local DataBase if I am running from XCode (simulator or connected iPhone).

And if its not running from XCode I would connect to my Web DataBase.

I've seen something like:

#if TARGET_IPHONE_SIMULATOR

but I'm not sure if it would work for simulating on device.

Upvotes: 1

Views: 467

Answers (2)

Andy Friese
Andy Friese

Reputation: 6509

You can determine if your app is being run under the debugger with the following code from Technical Q&A QA1361.

#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>

static bool AmIBeingDebugged(void)
    // Returns true if the current process is being debugged (either 
    // running under the debugger or has a debugger attached post facto).
{
    int                 junk;
    int                 mib[4];
    struct kinfo_proc   info;
    size_t              size;

    // Initialize the flags so that, if sysctl fails for some bizarre 
    // reason, we get a predictable result.

    info.kp_proc.p_flag = 0;

    // Initialize mib, which tells sysctl the info we want, in this case
    // we're looking for information about a specific process ID.

    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID;
    mib[3] = getpid();

    // Call sysctl.

    size = sizeof(info);
    junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
    assert(junk == 0);

    // We're being debugged if the P_TRACED flag is set.

    return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}

Tested successfully under simulator and device (iPhone 4, iOS 5.0.1).

Important Because the definition of the kinfo_proc structure (in ) is conditionalized by __APPLE_API_UNSTABLE, you should restrict use of the above code to the debug build of your program.

Upvotes: 3

hotpaw2
hotpaw2

Reputation: 70733

You could have the compiler select different code in your builds, depending on different pre-processor macros in your Debug versus Release Build settings. Debug could use local, and Release use the web.

Upvotes: 1

Related Questions