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

Related searches

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).

I obviously do use something like this ...

#import <TargetConditionals.h>


// Simulator specific code


// Device specific code


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 ...

Running Your App in the Simulator or on a Device, Launch your app in a simulated iOS, tvOS, or watchOS device, or on a device connected to For macOS apps, choose a scheme, then click the Run button in the toolbar. You can also run SwiftUI apps in the simulator or on a device using the A scheme is a collection of settings that specify the targets to build, the build� To run your app in Simulator, choose an iOS simulator—for example, iPhone 6 Plus, iPad Air, or iPhone 6 + Apple Watch - 38mm—from the Xcode scheme pop-up menu, and click Run. Xcode builds your project and then launches the most recent version of your app running in Simulator on your Mac screen, as shown in Figure 1-1 .

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?

How do you run an xcode project on…, https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/ When testing an app in Simulator, it is easiest to launch and run your app in Simulator to check recent discussions via search, first - example: Re: No Simulators or the according settings so I could try to fix this problem (it's the 14th issue so far)? iPhone simulator - how to detect when app is running on simulator (so can setup test data)? 415. dyld: Library not loaded: @rpath/libswiftCore.dylib. 4.

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
// code to run if not running on simulator


WatchConnectivity transferUserInfo…, I have a dependent watch app that use to work fine in simulation for Xcode 10.2.1 , x.x, it seem like the transfer data does not work anymore. Apple Watch simulator, so I have to run both targets, for WatchKit first and then run main app . I already double-check to use corrected pair simulators (corrected paired iPhone + � @StCredZero Perhaps that is because code compiled to run on the simulator can't run on a device and vice versa, so in the end it doesn't matter wether you do the check at compile time or at run time. – Johan Kool Mar 17 '14 at 4:14

The code block that worked for me:

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

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

Testing and Debugging in Simulator, Explains how to develop, debug, and test iOS and Watch apps on a Mac during development. For iOS, tvOS, and watchOS apps, you can choose a simulated device, under [Platform] Simulators, from the run destination menu next to the scheme menu in the toolbar. To add additional simulators of a product family running older versions of the operating system, choose Add Additional Simulators.

Xcode Overview: Running in Simulator, Adding 3D Scenes; Adding Data Sets; Adding Watch Complications Simulator enables you to simulate multiple iOS and watchOS devices Although you can test your app's basic behavior in Simulator, the your custom simulator configuration, choose a device type, and then choose an iOS version. 10. Now the App is installed to the simulator and you are ready to test. Happy testing! Building the App for iOS Simulator. If you want to generate a build and provide this App to someone to install to an iOS simulator using the above process, you should follow the steps detailed below. 1.

Platform: Mac Pricing plans: Free 5. Electric Mobile Studio. Electric Mobile Studio is a paid way to test and run iOS apps on Windows computers, but it offers a full product trial for 7 days

Also consider using Firebase Test Lab to run your app on a wide variety of real devices hosted in a cloud-based infrastructure. Set up a device for development. Before you can start debugging on your device, there are a few things you must do: On the device, open the Settings app, select Developer options, and then enable USB debugging.

  • 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.
  • 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.
  • 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?
  • NSUserDefaults are removed when you remove your application from simulator/device.
  • 2015 update: TARGET_IPHONE_SIMULATOR is now deprecated and TARGET_OS_SIMULATOR should be used instead.
  • 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.
  • Using this technique is fragile because it will require updating whenever Apple updates their platforms. The technique specified by robertvojta is more reliable.
  • 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.