NSUserDefault BoolKey未保存在关闭应用程序后重置

时间:2015-06-23 21:07:07

标签: ios objective-c

我用于启动我的应用程序下面的代码,但在某些设备中nsuserdefaults没有保存。当我从主页按钮终止应用程序并再次打开时,它启动就好像nsuserdefaults没有保存!

  NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
if([pref boolForKey:@"islogin"])
{


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    UITabBarController * tabbar = [storyboard instantiateViewControllerWithIdentifier:@"tabbar"];


    tabbar.selectedIndex = 2;
    self.window.rootViewController = tabbar;
    [self.window makeKeyAndVisible];
}else{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"register"];
    [self.window makeKeyAndVisible];

}

代码已从评论中移除:

[pref setObject:[response valueForKey:@"user_id"] forKey:@"user_id"];
[self performSegueWithIdentifier:@"code" sender:self];
[pref setBool:YES forKey:@"islogin"];

我没有添加同步,因为在我的设备中它运行正常,现在我添加同步,我正在等待结果。

1 个答案:

答案 0 :(得分:1)

您需要确保保存NSUserDefaults

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"islogin"];
[defaults synchronize];