需要检测NSUserDefaults?是否有对应的用户名,如果没有,就加载一个模式视图控制器。
问题是运行之后即使用户名是nil,检测的结果还是存在。
//Check if the login data exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"username"] != nil) {
//Load Login View if no username is found
NSLog(@"No username found");
LoginViewController *loginView = [[LoginViewController alloc] init];
[self presentViewController:loginView animated:YES completion:nil];
}
else {
NSString *savedUsername = [defaults stringForKey:@"username"];
NSLog(@"Username found: %@", savedUsername);
}
在NSLog中一直得到这个: Username found: (null)