iOS开发项目篇—18截取授权成功的请求标记
iOS开发项目篇—18截取授权成功的请求标记
一、步骤和说明
1 // 2 // YYOAuthViewController.m 3 // 4 5 #import "YYOAuthViewController.h" 6 7 @interface YYOAuthViewController () 8 9 @end 10 11 @implementation YYOAuthViewController 12 13 - (void)viewDidLoad 14 { 15 [super viewDidLoad]; 16 17 //1.创建UIWebView 18 UIWebView *webView=[[UIWebView alloc]init]; 19 webView.frame=self.view.bounds; 20 [self.view addSubview:webView]; 21 22 //2.加载登陆界面 23 NSURL *url=[NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=1972915028&redirect_uri=http://www.cnblogs.com/wendingding/"]; 24 NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url]; 25 [webView loadRequest:request]; 26 } 27 28 29 @end
YYAppDelegate.m文件代码:
1 #import "YYAppDelegate.h" 2 #import "YYTabBarViewController.h" 3 #import "YYNewfeatureViewController.h" 4 #import "YYOAuthViewController.h" 5 6 @implementation YYAppDelegate 7 8 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 9 { 10 11 //1.创建窗口 12 self.window=[[UIWindow alloc]init]; 13 self.window.frame=[UIScreen mainScreen].bounds; 14 15 //2.设置窗口的根控制器 16 17 self.window.rootViewController=[[YYOAuthViewController alloc]init]; 18 //3.显示窗口(主窗口) 19 [self.window makeKeyAndVisible]; 20 return YES; 21 }
模拟器运行:三个过程(显示登陆窗口,登陆,授权)
二、获取授权成功的请求标记
方法:设置代理,拦截发送的网络请求
代码:
1 #pragma mark-UIWebViewDelegate 2 /** 3 * UIWebView每当发送一个请求之前,都会先调用这个代理方法(询问代理允不允许加载这个请求) 4 * @param request 即将发送的请求 5 * @return YES允许加载,NO不允许加载 6 */ 7 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 8 { 9 //1.获得请求地址 10 NSString *urlStr=request.URL.absoluteString; 11 NSLog(@"%@",urlStr); 12 13 //2.判断url是否为回调地址 14 /* 15 https://api.weibo.com/oauth2/authorize?client_id=1972915028&redirect_uri=http://www.cnblogs.com/wendingding/ 16 https://api.weibo.com/oauth2/authorize 17 https://api.weibo.com/oauth2/authorize# 18 19 https://api.weibo.com/oauth2/authorize 20 range.location==(-1)NSNoFound 21 range.length==0 22 http://www.cnblogs.com/wendingding/?code=c3dca3b51ab954bac42ebdb253661e4d 23 range.location==0 24 range.length>0 25 */ 26 //urlStr在字符串中的范围 27 //设置从等号位置开始,不用再额外的找位置 28 NSRange range=[urlStr rangeOfString:@"http://www.cnblogs.com/wendingding/?code="]; 29 //判断是否为回调地址 30 if (range.location!=NSNotFound) {//是回调地址 31 //截取授权成功后的请求标记 32 int from=range.location+range.length; 33 NSString *code=[urlStr substringFromIndex:from]; 34 YYLog(@"%@--%@--",urlStr,code); 35 } 36 return YES; 37 }
打印查看:
获取请求标记代码:
YYOAuthViewController.m文件如下
1 // 2 // YYOAuthViewController.m 3 // 4 5 #import "YYOAuthViewController.h" 6 #import "MBProgressHUD+MJ.h" 7 8 @interface YYOAuthViewController ()<UIWebViewDelegate> 9 10 @end 11 12 @implementation YYOAuthViewController 13 14 - (void)viewDidLoad 15 { 16 [super viewDidLoad]; 17 18 //1.创建UIWebView 19 UIWebView *webView=[[UIWebView alloc]init]; 20 webView.frame=self.view.bounds; 21 [self.view addSubview:webView]; 22 23 24 //2.加载登陆界面 25 NSURL *url=[NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=1972915028&redirect_uri=http://www.cnblogs.com/wendingding/"]; 26 NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url]; 27 [webView loadRequest:request]; 28 29 //3.设置代理 30 webView.delegate=self; 31 } 32 33 #pragma mark-UIWebViewDelegate 34 /** 35 * UIWebView开始加载资源的时候调用(开始发送请求) 36 */ 37 -(void)webViewDidStartLoad:(UIWebView *)webView 38 { 39 [MBProgressHUD showMessage:@"正在努力加载中···"]; 40 } 41 42 /** 43 * UIWebView加载完毕的时候调用(请求结束) 44 */ 45 -(void)webViewDidFinishLoad:(UIWebView *)webView 46 { 47 [MBProgressHUD hideHUD]; 48 } 49 50 /** 51 * UIWebView加载失败的时候调用(请求失败) 52 */ 53 -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 54 { 55 [MBProgressHUD hideHUD]; 56 } 57 58 /** 59 * UIWebView每当发送一个请求之前,都会先调用这个代理方法(询问代理允不允许加载这个请求) 60 * @param request 即将发送的请求 61 * @return YES允许加载,NO不允许加载 62 */ 63 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 64 { 65 //1.获得请求地址 66 NSString *urlStr=request.URL.absoluteString; 67 NSLog(@"%@",urlStr); 68 69 //2.判断url是否为回调地址 70 /* 71 https://api.weibo.com/oauth2/authorize?client_id=1972915028&redirect_uri=http://www.cnblogs.com/wendingding/ 72 https://api.weibo.com/oauth2/authorize 73 https://api.weibo.com/oauth2/authorize# 74 75 https://api.weibo.com/oauth2/authorize 76 range.location==(-1)NSNoFound 77 range.length==0 78 http://www.cnblogs.com/wendingding/?code=c3dca3b51ab954bac42ebdb253661e4d 79 range.location==0 80 range.length>0 81 */ 82 //urlStr在字符串中的范围 83 //设置从等号位置开始,不用再额外的找位置 84 NSRange range=[urlStr rangeOfString:@"http://www.cnblogs.com/wendingding/?code="]; 85 //判断是否为回调地址 86 if (range.location!=NSNotFound) {//是回调地址 87 //截取授权成功后的请求标记 88 int from=range.location+range.length; 89 NSString *code=[urlStr substringFromIndex:from]; 90 YYLog(@"%@--%@--",urlStr,code); 91 92 //根据code获得一个accessToken 93 [self accessTokenWithCode:code]; 94 95 //禁止加载回调页面,拿到想要的东西就好了。 96 return NO; 97 } 98 return YES; 99 } 100 /** 101 * 根据code获得一个accessToken 102 * @param code 授权成功后的请求标记 103 */ 104 -(void)accessTokenWithCode:(NSString *)code 105 { 106 //处理操作.... 107 } 108 @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。