GTAccountManager.h
#import <Foundation/Foundation.h>
@interface GTAccountManager : NSObject
/**
* @author 黄健, 2016-05-24 11:05:24
*
* @brief 当前登录账号
*/
@property (nonatomic, strong) GTUser *currentUser;
/**
* @author 黄健, 2016-05-24 11:05:48
*
* @brief 创建管理当前登录账号的单例类
*
* @return 返回单例对象
*/
+ (instancetype)sharedManager;
/**
* @author 黄健, 2016-05-24 11:05:27
*
* @brief 注销账号
*/
- (void)resetUser;
/**
* @author 黄健, 2016-05-24 11:05:38
*
* @brief 判断是否已经登录
*
* @return 返回YES即表示已登录,返回NO表示没登录
*/
- (BOOL)isLogined;
@end
GTAccountManager.m
#import "GTAccountManager.h"
@implementation GTAccountManager
{
/**
* @author 黄健, 2016-05-24 11:05:10
*
* @brief 需要引入KeychainItemWrapper类
*/
KeychainItemWrapper *_keychain;
}
+ (instancetype)sharedManager
{
static GTAccountManager *mgr = nil;
static dispatch_once_t onceTonken;
dispatch_once(& onceTonken, ^{
if(mgr == nil) {
mgr = [[GTAccountManager alloc ]init];
}
});
return mgr;
}
- (instancetype)init
{
if (self = [super init]) {
_currentUser = [[GTUser alloc] init];
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
_keychain = [[KeychainItemWrapper alloc] initWithIdentifier:identifier accessGroup:nil];
_currentUser.mobile_phone = [_keychain objectForKey:(__bridge id)(kSecAttrAccount)];
_currentUser.password = [_keychain objectForKey:(__bridge id)(kSecValueData)];
}
return self;
}
- (void)setCurrentUser:(GTUser *)currentUser
{
_currentUser = currentUser;
[_keychain setObject:currentUser.mobile_phone forKey:(__bridge id)(kSecAttrAccount)];
[_keychain setObject:currentUser.password forKey:(__bridge id)(kSecValueData)];
}
- (void)resetUser
{
_currentUser = nil;
[_keychain resetKeychainItem];
}
- (BOOL)isLogined
{
if (_currentUser.mobile_phone.length && _currentUser.password.length) {return YES;} return NO;
}
@end