Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...