第一篇技术blog--账号管理体系

每一个应用都有自己的账号管理体系AccountManager,涉及到的类还有:

AccountStorage, UserdefaultManager, UserInfo, SSKeychain, FileHelper.

一、AccountStorage,主要是1、通过accountId初始化账号,2、保存用户信息到文件中 (用到的对象类:NSKeyedUnarchiver, NSKeyedArchiver)

- (instancetype)initWithAccountId:(NSNumber*)accountId;

- (BOOL)saveUserInfo:(CLGUserInfo*)userInfo;

二、UserdefaultManager, 1、保存是否第一次启动应用;2、保存最近一次登录该应用的账号,手机号及accountId,(密码保存到userdefault不安全)

@property(readonly)NSNumber*hasLaunched;

@property(readonly)NSNumber*latestLoginAccountId;

@property(copy,readonly)NSString*latestLoginPhone;

- (void)saveHasLaunched:(NSNumber*)hasLaunched;

- (void)saveLatestLoginPhone:(NSString*)latestLoginPhone;

- (void)saveLatestLoginAccountId:(NSNumber*)latestLoginAccountId;

三、UserInfo, 用来保存用户的一些基本信息的模型;

四、SSKeychain, Xcode自带的一套保存用户密码的体系。对SSKeyChain中的password,service,account的理解和使用。password,密码;account,账号,一般是手机号;service,保存的是服务的类型,就是用户名和密码是为什么应用保存的一个标志(service是一个字符串,一般用bundle ID)。

[SSKeychainsetPassword:password forService:kServiceForKeychain account:loginPhone];

五、FileHelper, 文件管理助手,每次登录或切换用户用到,管理相应账号的本地文件的保存路径;包括相应账号下的聊天记录,音频,图片等文件路径。

*返回当前登录帐号对应的根目录

+ (NSString*)currentDirectoryPath;

*根据accountId改变currentDirectoryPath,每次登录或切换账号用到

+ (BOOL)changeCurrentDirectoryPathWithAccountId:(NSNumber*)accountId;

*获取媒体文件(如音频,图片文件)路径名,为绝对路径

+ (NSString*)conversationMediaFilePathNameForConversationId:(NSString*)conversationId;

六、重头戏?AccountManager, 

+ (instancetype)shareManager;

*注册

- (void)registerWithPhone:(NSString*)phone password:(NSString*)password Success:(void(^)(NSString*successMsg))success failure:(void(^)(NSString*failureMsg))failure;

*自动登录,用户登录过后,下次登录时直接调用该方法

- (void)autoLoginSuccess:(void(^)(NSString*successMsg))success failure:(void(^)(NSString*failureMsg))failure;

*登录,包括请求登录接口,同步用户数据

- (void)loginWithPhone:(NSString*)phone password:(NSString*)password success:(void(^)(NSString*successMsg))success failure:(void(^)(NSString*failureMsg))failure;

(每次登录要更新当前账号信息,用到FileHelp:[self updateCurrentAccountWithUserInfo:userInfo];)

*保存最近登录的电话号码和密码

- (void)saveLatestLoginPhone:(NSString*)loginPhone password:(NSString*)password;

*登出并且清理当前账号

- (void)logoutAndClearCurrentAccountSuccess:(void(^)(NSString*successMsg))success failure:(void(^)(NSString*failureMsg))failure;

纯过程的展示,没有自己的理解,写的不好,勿喷。

你可能感兴趣的:(第一篇技术blog--账号管理体系)