2010-09-21 66 views
0

在我的iPad应用程序中,我有一个UIAlertView,它在启动时弹出,但是我只想让它在用户第一次启动应用程序时弹出。它的设置提示,说,你第一次,你想设置?第一次打开事件

我该怎么做?我已经听到最好写出一个plist文件,并保存一个布尔值,但我将如何解决这个问题?

谢谢。

回答

3

修改下面的代码,以满足您的需求;你可以把它放在你的根视图控制器的viedDidLoad方法中。该代码会跟踪应用程序的第一次运行,启动次数以及您的设置提示是否已显示给用户。

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]; 

}