【发布时间】:2010-09-21 10:43:21
【问题描述】:
在我的 iPad 应用程序中,我有一个 UIAlertView,它会在启动时弹出,但我只希望它在用户第一次启动应用程序时弹出。是设置提示,说你是第一次,要设置吗?
我该怎么做?我听说最好写出一个 plist 文件并保存一个 bool 值,但我该如何解决这个问题?
【问题讨论】:
标签: iphone objective-c ipad plist uialertview
在我的 iPad 应用程序中,我有一个 UIAlertView,它会在启动时弹出,但我只希望它在用户第一次启动应用程序时弹出。是设置提示,说你是第一次,要设置吗?
我该怎么做?我听说最好写出一个 plist 文件并保存一个 bool 值,但我该如何解决这个问题?
【问题讨论】:
标签: iphone objective-c ipad plist uialertview
修改以下代码以满足您的需要;你可以把它放在你的根视图控制器 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];
}
【讨论】:
您只需几行代码即可使用 NSUserDefaults 实现此目的。
【讨论】: