开发者

First Time Opened Event

In my iPad app, I have a UIAlertView which pops up upon start up, however I only want this to popup the very first time the user starts the application. It's a setup prompt, saying, it's your first time, would you like to setup?

How can I do this? I have heard it'开发者_运维百科s best to write out to a plist file and save a bool value, but how would I tackle this?


Modify the following code to suit your needs; you may put it in your root view controller viedDidLoad method. The code keeps track of the first run of the application, of the number of launches and whether or not the your setup prompt has been shown to the user.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![defaults objectForKey:@"firstRun"]) {

    // this is the first run 
    // store this information

    [defaults setObject:[NSDate date] forKey:@"firstRun"];
    [defaults setInteger:1 forKey:@"launches"];
    [defaults setBool:NO forKey:@"setupPromptHasBeenShown"];
    [defaults synchronize];

    // now prompt the user to setup the app 
    // once the the prompt has been shown, 
    // if the user actually decides to setup the app, 
    // store this information again, so you will not prompt him/her again
    [defaults setBool:YES forKey:@"setupPromptHasBeenShown"];
    [defaults synchronize];

}
else{
     // this is not the first run
     NSInteger daysSinceInstall = [[NSDate date] timeIntervalSinceDate:[defaults objectForKey:@"firstRun"]] / 86400;
     NSInteger launches = [defaults integerForKey:@"launches"];
     [defaults setInteger:launches+1 forKey:@"launches"];
     [defaults synchronize];

}


You could use NSUserDefaults to achieve this with just a few lines of code.

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜