用户登录后,存储用户信息,注销
存储一些用户额外信息到偏好
static NSString * const kKeyUserId = @"id"; static NSString * const kKeyUsername = @"username"; static NSString * const kKeyName = @"name"; static NSString * const kKeyBio = @"bio"; static NSString * const kKeyWeibo = @"weibo"; static NSString * const kKeyBlog = @"blog"; static NSString * const kKeyThemeId = @"theme_id"; static NSString * const kKeyCreatedAt = @"created_at"; static NSString * const kKeyState = @"state"; static NSString * const kKeyPortrait = @"new_portrait"; static NSString * const kKeyEmail = @"email"; static NSString * const kKeyPrivateToken = @"private_token"; static NSString * const kKeyAdmin = @"is_admin"; static NSString * const kKeyCanCreateGroup = @"can_create_group"; static NSString * const kKeyCanCreateProject = @"can_create_project"; static NSString * const kKeyCanCreateTeam = @"can_create_team"; static NSString * const kKeyFollow = @"follow"; @implementation User + (void)saveUserInformation:(GLUser *)user { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setFloat:user.userId forKey:kKeyUserId]; [userDefaults setObject:user.username forKey:kKeyUsername]; [userDefaults setObject:user.name forKey:kKeyName]; [userDefaults setObject:user.bio forKey:kKeyBio]; [userDefaults setObject:user.weibo forKey:kKeyWeibo]; [userDefaults setObject:user.blog forKey:kKeyBlog]; [userDefaults setInteger:user.themeId forKey:kKeyThemeId]; [userDefaults setObject:user.state forKey:kKeyState]; [userDefaults setObject:user.createdAt forKey:kKeyCreatedAt]; [userDefaults setObject:user.portrait forKey:kKeyPortrait]; [userDefaults setObject:user.email forKey:kKeyEmail]; [userDefaults setObject:user.private_token forKey:kKeyPrivateToken]; [userDefaults setBool:user.admin forKey:kKeyAdmin]; [userDefaults setBool:user.canCreateGroup forKey:kKeyCanCreateGroup]; [userDefaults setBool:user.canCreateProject forKey:kKeyCanCreateProject]; [userDefaults setBool:user.canCreateTeam forKey:kKeyCanCreateTeam]; [userDefaults setObject:user.follow forKey:kKeyFollow]; [userDefaults synchronize]; }
通过服务和邮箱来存储密码到钥匙串
+ (void)saveAccount:(NSString *)email andPassword:(NSString *)password { [SSKeychain setPassword:password forService:@"Git@OSC" account:email]; }
需要时通过服务和邮箱取出密码
- (void)viewDidAppear:(BOOL)animated { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSString *email = [userDefaults objectForKey:@"email"]; NSString *password = [SSKeychain passwordForService:@"Git@OSC" account:email]; _accountTextField.text = email ?: @""; _passwordTextField.text = password ?: @""; if (!_accountTextField.text.length || !_passwordTextField.text.length) { submit.alpha = 0.4; submit.enabled = NO; } }
注销操作,没有删除偏好里的邮箱以及钥匙串里的密码,
#pragma mark - logout - (void)logout { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults removeObjectForKey:kKeyPrivateToken]; [userDefaults removeObjectForKey:kKeyUserId]; [userDefaults removeObjectForKey:kKeyUsername]; [userDefaults removeObjectForKey:kKeyName]; [userDefaults removeObjectForKey:kKeyBio]; [userDefaults removeObjectForKey:kKeyWeibo]; [userDefaults removeObjectForKey:kKeyBlog]; [userDefaults removeObjectForKey:kKeyThemeId]; [userDefaults removeObjectForKey:kKeyState]; [userDefaults removeObjectForKey:kKeyCreatedAt]; [userDefaults removeObjectForKey:kKeyPortrait]; [userDefaults removeObjectForKey:kKeyPrivateToken]; [userDefaults removeObjectForKey:kKeyAdmin]; [userDefaults removeObjectForKey:kKeyCanCreateGroup]; [userDefaults removeObjectForKey:kKeyCanCreateProject]; [userDefaults removeObjectForKey:kKeyCanCreateTeam]; [userDefaults removeObjectForKey:kKeyFollow]; // 删除用户动态及项目的缓存 NSUserDefaults *cache = [NSUserDefaults standardUserDefaults]; for (int i = 3; i < 6; i++) { NSString *key = [NSString stringWithFormat:@"type-%d", i]; [cache removeObjectForKey:key]; } [cache removeObjectForKey:@"type-9"]; //[self.navigationController popToRootViewControllerAnimated:YES]; ProjectsViewController *projectViewController = [ProjectsViewController new]; UINavigationController *front = [[UINavigationController alloc] initWithRootViewController:projectViewController]; [self.revealController setFrontViewController:front]; [self.revealController showViewController:self.revealController.frontViewController]; }