我用于启动我的应用程序下面的代码,但在某些设备中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"];
我没有添加同步,因为在我的设备中它运行正常,现在我添加同步,我正在等待结果。
答案 0 :(得分:1)
您需要确保保存NSUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"islogin"];
[defaults synchronize];