iPhone simulator - how to detect when app is running on simulator (so can setup test data)?

20,159

Solution 1

I obviously do use something like this ...

#import <TargetConditionals.h>

#if TARGET_IPHONE_SIMULATOR

// Simulator specific code

#else // TARGET_IPHONE_SIMULATOR

// Device specific code

#endif // TARGET_IPHONE_SIMULATOR

And to your second question ... Something like this should help you. In your app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  if ( ! [[NSUserDefaults standardUserDefaults] boolForKey:@"initialized"] ) {
    // Setup stuff
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"initialized"];
  }

  ... your code ...
}

Solution 2

Swift 5:

TARGET_OS_SIMULATOR does not work in Swift 5. targetEnvironment(simulator) works, like below:

#if targetEnvironment(simulator)
// code to run if running on simulator
#else
// code to run if not running on simulator
#endif

Reference

Solution 3

If you'd like to check on runtime (instead compile time with the # compiler macro) use this code:

UIDevice *currentDevice = [UIDevice currentDevice];
if ([currentDevice.model rangeOfString:@"Simulator"].location == NSNotFound) {
    //running on device
} else { 
    // running in Simulator
}

see also this question: How can I programmatically determine if my app is running in the iphone simulator?

Solution 4

The code block that worked for me:

#if defined(__i386__)  || defined(__x86_64__)
    /* Run code if in Simulator */
#else
    /* Run code if in device */
#end

I noticed __i386__ does not work for iPhone 6 simulators, so I added x86_64

Share:
20,159

Related videos on Youtube

Greg
Author by

Greg

Updated on July 09, 2022

Comments

  • Greg
    Greg almost 2 years

    Any sample code that would show me how to, in my iPhone application code:

    1. How to detect if the application has just been DEPLOYED to be run to a simulator (and not a device) [if "Deployed" isn't available, then just detecting when the app is being run on the simulator as opposed to a device)
    2. Where about in my iPhone app code would I put the lines that setup my test data in the simulator - this is noting I wanted the test data to be effectively wiped clean/re-instated each time I recompile and push to the simulator (however I didn't really want this code to be run during my usage of the application in the simulator - e.g. should be able to swap apps in the simulator & then when I start my app again in the simulator it should not run the data setup code

    Any other better suggestions re how to cover off managing this test data on the simulator would be good. Background here is that I'm talking about test data in the Calendar (e.g. using Event Kit), so I don't want to have the app putting calendar items into my iPhone when I deploy to my device (sorry - only have 1 personal iPhone here).

  • Greg
    Greg about 13 years
    thanks - so re 2: where in the code (i.e. which method) could I put this setup code so it only runs when I start the app for the first time? i.e. it doesn't run again if I jump to App B then back to my App. However I assume when I killed the app off (double clicking on iPhone button etc) that when I clicked on the app again this would be classed as running for the first time. Hope this makes sense.
  • zrzka
    zrzka about 13 years
    Just edited my answer. You need some flag to know if your application was initialized or not. I did show you how to do this with user defaults, but there many ways how to do this.
  • Greg
    Greg about 13 years
    ok thanks - so you're implying there's nothing in an iPhone life-cycle model (i.e. with states, messages) that inherently differentiates then? i.e. you pretty much have to code it yourself? - Also if you did (say on the simulator) delete the application (i.e. kill the process I guess), then would this code work here? i.e. would it really remove NSUserDefaults?
  • zrzka
    zrzka about 13 years
    NSUserDefaults are removed when you remove your application from simulator/device.
  • zrzka
    zrzka about 13 years
    Killing process doesn't mean application delete. It just stops working and you can start it again, even from Xcode.
  • Dev-iL
    Dev-iL over 8 years
    2015 update: TARGET_IPHONE_SIMULATOR is now deprecated and TARGET_OS_SIMULATOR should be used instead.
  • gilm
    gilm over 8 years
    This is no longer correct. currentDevice.model will now return "iPhone" or "iPad". I guess this has changed in recent SDK updates. We use sysctlbyname("hw.machine") and compare return value to x86_64.
  • ThomasW
    ThomasW over 8 years
    Using this technique is fragile because it will require updating whenever Apple updates their platforms. The technique specified by robertvojta is more reliable.
  • Roozbeh Zabihollahi
    Roozbeh Zabihollahi over 8 years
    That's true. But easier to use, because you do not need an import. Also, it is not that common for Apple to add a new platform.
  • pieroxy
    pieroxy over 2 years
    I guess it doesn't work anymore in M1 Macs. Please update your answer.
  • Roozbeh Zabihollahi
    Roozbeh Zabihollahi about 2 years
    @pieroxy what would be the equivalent in M1?
  • pieroxy
    pieroxy about 2 years
    @RoozbehZabihollahi I haven't got the slightlest clue. This is so complicated and unreliable that I think we should not try to guess, it's just a disaster waiting to happen.
  • keccs
    keccs almost 2 years
    This still works on M1 macs, since currently the iOS Simulator is an X86 application. If you're using Swift, the same is written like this: #if arch(i386) || arch(x86_64) ...