#import "ViewController.h" #define kAppKey @"706003107" #define kAppSecret @"53b3f1774f9a050e1d4931bab3b55633" #define kRedirect_url @"www.baidu.com" #define kAccessTokenKey @"kAccessTokenKey" #define kExpiresTimeKey @"kExpiresTimeKey" #define kUserIDKey @"kUserIDKey" @interface ViewController ()<UIWebViewDelegate> @property (nonatomic,strong) UIWebView *webView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)loginDidClicked:(id)sender { //使用oAuth的oauth2发起请求,拼接网址,,参数开始用?,&连接不同的参数, NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",kAppKey,kRedirect_url]; NSURL *url = [NSURL URLWithString:urlString]; //发送请求-创建session // NSURLSession *session = [NSURLSession sharedSession]; //创建请求,网页 NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60]; //使用UIWebView加载页面 self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds]; _webView.delegate = self; [self.view addSubview:_webView]; [_webView loadRequest:request]; } - (IBAction)logout:(id)sender { NSUserDefaults *useDefaults = [NSUserDefaults standardUserDefaults]; [useDefaults setObject:nil forKey:kAccessTokenKey]; [useDefaults setObject:nil forKey:kExpiresTimeKey]; [useDefaults setObject:nil forKey:kUserIDKey]; [useDefaults synchronize]; } #pragma mark --------------- -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ //目标获取code的状态码 解决方案,对即将加载的request对应的URL进行解析,一旦URL里面包含code=,那么就停止 NSRange range = [[request.URL relativeString]rangeOfString:@"code="]; if (range.length!=0) { //说明此刻即将请求的request里面包含了code,解析code后面的内容, NSString *code = [[request.URL relativeString]substringFromIndex:range.location+range.length]; //将界面webView 删掉 [self.webView removeFromSuperview]; //使用code去换取一个accessToken,解决方案,想服务器发送一个post请求 NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/access_token"]; //创建一个可变的请求 NSMutableURLRequest *upRequest = [NSMutableURLRequest requestWithURL:url]; //设置请求的方式为post [upRequest setHTTPMethod:@"POST"]; //创建session NSURLSession *session = [NSURLSession sharedSession]; //准备上传的数据,将上传的参数拼接成一个字符串 NSString *dataString = [NSString stringWithFormat:@"client_id=%@&client_secret=%@&grant_type=authorization_code&code=%@&redirect_uri=%@",kAppKey,kAppSecret,code,kRedirect_url]; //将字符串转化为NSdata NSData *upData = [dataString dataUsingEncoding:NSUTF8StringEncoding]; //创建一个uploadTask NSURLSessionUploadTask *upLoadTask = [session uploadTaskWithRequest:upRequest fromData:upData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { //NSDATA 转化为NSDictionary NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",resultDic); //保存access_token,expires_in uid NSString *accessToken = [resultDic objectForKey:@"access_token"]; NSString *expiresIn = [resultDic objectForKey:@"expires_in"]; NSString *uid = [resultDic objectForKey:@"uid"]; NSUserDefaults *useDefaults = [NSUserDefaults standardUserDefaults]; [useDefaults setObject:accessToken forKey:kAccessTokenKey]; [useDefaults setObject:expiresIn forKey:kExpiresTimeKey]; [useDefaults setObject:uid forKey:kUserIDKey]; [useDefaults synchronize]; }]; [upLoadTask resume]; return NO; }else{ return YES; } } @end