1.创建webView加载授权页面
2.发送请求 获取AccessToken func loadAccessToken(code: String) -> RACSignal
3.把AccessToken再发送给服务器 获取用户信息 func loadUserInfo(uid: String) -> RACSignal
4.把用户信息本地化归档存储 account.saveUserAccount()
5.通知控制器跳转页面 加载用户主界面
*实现NSCoding协议 以实现归档
// MARK: - NSCoding // 归档,将当前对象保存到磁盘之前,转换成二进制数据,跟序列化很像 func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeObject(access_token, forKey: "access_token") aCoder.encodeObject(expiresDate, forKey: "expiresDate") aCoder.encodeObject(uid, forKey: "uid") aCoder.encodeObject(name, forKey: "name") aCoder.encodeObject(avatar_large, forKey: "avatar_large") } // 解档,将二进制数据从磁盘加载,转换成自定义对象时调用,跟反序列化很像 required init?(coder aDecoder: NSCoder) { access_token = aDecoder.decodeObjectForKey("access_token") as? String expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate uid = aDecoder.decodeObjectForKey("uid") as? String name = aDecoder.decodeObjectForKey("name") as? String avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String }
*保存账户信息 & 读取用户信息
/// 将当前对象归档保存 func saveUserAccount() { // 对象函数中,调用静态属性,使用`类名.属性` printLog("保存路径 " + UserAccount.accountPath) // `键值`归档 NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.accountPath) } /// 加载用户账户 /// /// - returns: 账户信息,如果用户还没有登录,返回 nil class func loadUserAccount() -> UserAccount? { // 解档加载用户账户的时候,需要判断 token 的有效期 let account = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? UserAccount if let date = account?.expiresDate { // 比较日期 date > NSDate() 结果是降序 if date.compare(NSDate()) == NSComparisonResult.OrderedDescending { return account } } return nil }
/