ios*_*nyc 32 iphone iphone-sdk-3.0
我想检查我的iPhone应用程序是否第一次运行.我可以在documents文件夹中创建一个文件并检查该文件以查看这是否是第一次运行应用程序,但我想知道是否有更好的方法来执行此操作.
pau*_*erd 69
我喜欢使用NSUserDefaults来存储第一次运行的指示.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:@"firstRun"])
[defaults setObject:[NSDate date] forKey:@"firstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
Run Code Online (Sandbox Code Playgroud)
然后你可以稍后测试一下......
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:@"firstRun"])
{
// do something or not...
}
Run Code Online (Sandbox Code Playgroud)
好吧,让我感到困惑的是用户默认值.
它们存放在哪里?
这是常见用例:检查是否存在值,例如firstRun.第一次它不会存在,所以通常接着设置值.第二次运行 - 下一个循环它确实存在,并触发其他用例/ else stmt
- - .H
@interface MyAppDelegate : UIResponder <UIApplicationDelegate>
//flag to denote if this is first time the app is run
@property(nonatomic) BOOL firstRun;
Run Code Online (Sandbox Code Playgroud)
------ .m
@implementation MyAppDelegate
@synthesize firstRun = _firstRun;
Run Code Online (Sandbox Code Playgroud)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//==============
//Check to see if this is first time app is run by checking flag we set in the defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:@"firstRun"]){
//flag doesnt exist then this IS the first run
self.firstRun = TRUE;
//store the flag so it exists the next time the app starts
[defaults setObject:[NSDate date] forKey:@"firstRun"];
}else{
//flag does exist so this ISNT the first run
self.firstRun = FALSE;
}
//call synchronize to save default - where its saved is managed by iOS - varies by device and iOS/Mac
[[NSUserDefaults standardUserDefaults] synchronize];
//TO TEST: delete the app on the device/simulator
//run it - should be the first run
//close it - make sure you kill it and its not just in the background else didFinishLaunchingWithOptions wont be called
//just applicationDidBecomeActive
//2nd run it should self.firstRun = FALSE;
//=============
Run Code Online (Sandbox Code Playgroud)
//注意重要如果年轻的ROOTVIEWCONTROLLER检查appDelegate.firstRun,请确保在上面设置self.window.rootViewController之前进行检查
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
----使用旗帜
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.firstRun){
NSLog(@"IS FIRST RUN - Do something: e.g. set up password");
}else {
NSLog(@"FPMyMusicScreenViewController: IS NOT FIRST RUN - Prompt for password");
}
Run Code Online (Sandbox Code Playgroud)
上面的例子让我感到困惑,因为他们第一次展示了如何检查它,但后来提到了如何在同一评论中"检查它".问题是,当我们发现它不存在时,我们立即创建并同步.因此,检查它的时间实际上意味着当您重新启动APP而不是与首次运行相同的运行时.
在您的app delegate中注册一个默认值:
NSDictionary *defaultsDict =
[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"FirstLaunch", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict];
[defaultsDict release];
Run Code Online (Sandbox Code Playgroud)
然后你要检查它:
NSUserDefaults *sharedDefaults = [NSUserDefaults standardUserDefaults];
if ([sharedDefaults boolForKey:@"FirstLaunch"]) {
//Do the stuff you want to do on first launch
[sharedDefaults setBool:NO forKey:@"FirstLaunch"];
[sharedDefaults synchronize];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18749 次 |
| 最近记录: |