Reputation: 34818
Any sample code that would show me how to, in my iPhone application 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).
Upvotes: 27
Views: 22320
Reputation:
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
Upvotes: 16
Reputation: 7347
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
Upvotes: 2
Reputation: 3414
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?
Upvotes: 12
Reputation: 21249
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 ...
}
Upvotes: 50