我想在應用第一次啓動時只打開一次ViewController。以下是我的代碼。問題是當我寫入YES爲NSUserdefaults,進行同步,然後關閉應用程序。在使用模擬器的Xcode中,值不會更新爲是。無法保存NSUserDefaults值iOS
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunch"])
{
// app already launched
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO],@"isFirstLaunch", nil]];
[[NSUserDefaults standardUserDefaults]synchronize];
}
else
{
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"isFirstLaunch", nil]];
[[NSUserDefaults standardUserDefaults]synchronize];
}
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunch"])
{
NSLog(@"first time");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
LASplashUserSettingViewController *splash = [storyboard instantiateViewControllerWithIdentifier:@"splash"];
[self.window.rootViewController presentViewController:splash animated:NO completion:nil];//
}
}
Why is NSUserDefaults not saving my values?
以上的有跟隨等多個環節。我在這裏錯過了什麼? 謝謝,
該鏈接非常有用..謝謝Caleb .. – LUI