博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
新浪微博客户端(15)-保存用户名和密码
阅读量:5251 次
发布时间:2019-06-14

本文共 7568 字,大约阅读时间需要 25 分钟。

检测本地是否保存有用户的密码。如果有,则下次自动登录;如果没有,则提示用户登录。

DJAccount.h

#import 
@interface DJAccount : NSObject/** 访问token */@property (nonatomic,copy) NSString *access_token;/** 过期时间,单位是秒 */@property (nonatomic,copy) NSNumber *expires_in;/** 当前user id */@property (nonatomic,copy) NSString *uid;/** 当前帐号创建时间 */@property (nonatomic,strong) NSDate *create_time;+ (instancetype)accountWithDictionary:(NSDictionary *)dict;@end

 

DJAccount.m

#import "DJAccount.h"@interface DJAccount() 
@end@implementation DJAccount+ (instancetype)accountWithDictionary:(NSDictionary *)dict { DJAccount *account = [[DJAccount alloc] init]; account.access_token = dict[@"access_token"]; account.expires_in = dict[@"expires_in"]; account.uid = dict[@"uid"]; account.create_time = [NSDate date]; return account;}- (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:self.access_token forKey:@"access_token"]; [encoder encodeObject:self.expires_in forKey:@"expires_in"]; [encoder encodeObject:self.uid forKey:@"uid"]; [encoder encodeObject:self.create_time forKey:@"create_time"]; }- (instancetype)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.access_token = [decoder decodeObjectForKey:@"access_token"]; self.expires_in = [decoder decodeObjectForKey:@"expires_in"]; self.uid = [decoder decodeObjectForKey:@"uid"]; self.create_time = [decoder decodeObjectForKey:@"create_time"]; } return self;}@end

 

DJAccountTool.h

#import 
@class DJAccount;@interface DJAccountTool : NSObject+ (void)saveAccount:(DJAccount *)account;+ (DJAccount *)account;@end

DJAccountTool.m

#import "DJAccountTool.h"#import "DJAccount.h"#define DJAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.archiver"]@implementation DJAccountTool+ (void)saveAccount:(DJAccount *)account {    [NSKeyedArchiver archiveRootObject:account toFile:DJAccountPath];    }+ (DJAccount *)account {            DJAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:DJAccountPath];    // 判断当前帐号是否过期    long long expires_in = [account.expires_in longLongValue];    // 获得过期时间    NSDate *expires_time = [account.create_time dateByAddingTimeInterval:expires_in];    // 获得当前时间    NSDate *now_time = [NSDate date];    // 如果now大于expires_time,则代表过期    NSComparisonResult result = [expires_time compare:now_time];    if (result != NSOrderedDescending) {        return nil;    }    return account;}@end

UIWindow+Extension.m

#import "UIWindow+Extension.h"#import "DJMainViewController.h"#import "DJNewFeatureViewController.h"@implementation UIWindow (Extension)+ (void)switchRootViewController {    // 获取当前主窗口    UIWindow *window = [UIApplication sharedApplication].keyWindow;     NSString *versionKey = @"CFBundleVersion";    // 从Info.plist中读取当前软件版本号    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[versionKey];    // 从沙盒中读取保存的历史版本号    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:versionKey];        // 判断当前软件版本号是否与沙盒中保存的一致    if ([currentVersion isEqualToString:lastVersion]) { // 版本号一致        DJMainViewController *mainVc = [[DJMainViewController alloc] init];        window.rootViewController = mainVc;    } else { // 版本号不一致,显示新特性,并将当前软件版本号保存到沙盒        /* 1.显示新特性 */        DJNewFeatureViewController *newVc = [[DJNewFeatureViewController alloc] init];        window.rootViewController = newVc;        /* 2.将当前版本号写入到沙盒 */        [[NSUserDefaults standardUserDefaults] setValue:currentVersion forKey:versionKey];        // 立即将内存中的数据同步到沙盒        [[NSUserDefaults standardUserDefaults] synchronize];    }}@end

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {                // 1. 创建窗口    self.window = [[UIWindow alloc] init];    self.window.frame = [UIScreen mainScreen].bounds;        // 2. 显示窗口    [self.window makeKeyAndVisible];    // 3. 判断当前帐户是否已经被授权来决定根控制器显示    DJAccount *account = [DJAccountTool account];    if (account) { // 之前已经成功授权        [UIWindow switchRootViewController];    } else { // 尚未成功授权       self.window.rootViewController = [[DJOAuthViewController alloc] init];    }        return YES;}

 

DJOAuthViewController.m

#import "DJOAuthViewController.h"#import "AFNetworking.h"#import "DJAccount.h"#import "DJAccountTool.h"#import "MBProgressHUD+MJ.h"@interface DJOAuthViewController () 
@end@implementation DJOAuthViewController- (void)viewDidLoad { [super viewDidLoad]; /* client_id&redirect_uri */ NSString *client_id = @"249054863"; NSString *redirect_uri = @"https://www.baidu.com"; UIWebView *webView = [[UIWebView alloc] init]; webView.frame = self.view.bounds; webView.delegate = self; [self.view addSubview:webView]; NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",client_id,redirect_uri]; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; [webView loadRequest:urlRequest];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - webView 代理方法- (void)webViewDidStartLoad:(UIWebView *)webView { [MBProgressHUD showMessage:@"加载中..."];}- (void)webViewDidFinishLoad:(UIWebView *)webView { [MBProgressHUD hideHUD];}/** 此方法可用于拦截http请求 */- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *urlString = request.URL.absoluteString; DJLog(@"current access url str : %@",urlString); // 1. 判断当前地址是否是回调地址(https://www.baidu.com/?code=27a3d9fb9bbc5d3c20be9ae8e4331b02) NSRange range= [urlString rangeOfString:@"code="]; if (range.length != 0) { // 是回调地址 // 2.截取code=后面的参数值 NSUInteger fromIndex = range.location + range.length; NSString *code = [urlString substringFromIndex:fromIndex]; // code 就是授权成功的请求标记 // 3.使用授权成功的请求标记(code)来换取accessToken [self getAccessTokenWithCode:code]; // 4.禁止加载回调地址 return NO; } return YES;}- (void)getAccessTokenWithCode:(NSString *)code { // 1. 创建请求管理者 AFHTTPSessionManager *requestManager = [AFHTTPSessionManager manager]; // 2. 配置请求参数 NSString *urlString = @"https://api.weibo.com/oauth2/access_token"; // 请求授权的access_token URL NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"client_id"] = @"249054863"; params[@"client_secret"] = @"71d5b761bac9f377af3b938f6d89ba85"; params[@"grant_type"] = @"authorization_code"; params[@"code"] = code; params[@"redirect_uri"] = @"https://www.baidu.com"; // 3. 发送请求 [requestManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable dict) { [MBProgressHUD hideHUD]; // 1. 将返回的字典转换为模型 DJAccount *account = [DJAccount accountWithDictionary:dict]; // 2. 存储当前帐号信息 [DJAccountTool saveAccount:account]; // 3. 切换根控制器到DJMainViewController [UIWindow switchRootViewController]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [MBProgressHUD showError:@"授权失败"]; DJLog(@"failure responseObject: %@",error); }]; }@end

 

最终效果:

 

转载于:https://www.cnblogs.com/yongdaimi/p/6005673.html

你可能感兴趣的文章
java取消_Java任务取消方案
查看>>
java double 显示0_Java Double初始化为0.0
查看>>
炉石java版_炉石传说 java实现
查看>>
linux cmake编译安装mysql_cmake编译安装MySQL-5.5
查看>>
mysql软件发布日期_MySQL获取今天的日期
查看>>
java读取docx_Java读取doc、docx、xls、xlsx、ppt、pptx、pdf文件内容
查看>>
python求向量函数的雅可比矩阵_[数学] 向量函数的雅可比矩阵与链式法则
查看>>
ip的正则表达式 java_Java用正则表达式判断是否为IP
查看>>
java里ojek_KEMANA - Open Source Ojek Online (like uber or gojek)
查看>>
java json asc排序_Java中对jsonArray的排序,使用的是Gson
查看>>
java高低八位反转_Java程序反转正整数的位
查看>>
vs建立java工程文件目录_初学VS的目录结构
查看>>
java线程池创建守护线程_多线程 线程池 守护线程
查看>>
java自动扩展_自动扩展背景图像
查看>>
java获取mp4头部信息_java调用ffmpeg获取视频文件信息的一些参数
查看>>
java 线程异步回调函数_并发编程~~~多线程~~~阻塞,非阻塞,同步,异步, 同步调用,异步调用, 异步调用 + 回调函数...
查看>>
java1010矩阵 类_JAVA_大数的阶乘-----蒜头君对阶乘产生了兴趣,他列出了前 1010 个正整数的阶乘以及对应位数的表...
查看>>
php7的redis扩展_CentOS7为php7.2安装php-redis扩展
查看>>
matlab播放视频代码,matlab实现视频中的目标跟踪,求大神帮忙翻译下代码!!
查看>>
l2正则化java代码,pytorch 实现L2和L1正则化regularization的操作
查看>>