#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()<UIWebViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.delegate = self;
[self.view addSubview:webView];
NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=500210150&redirect_uri=http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
//可以改变request
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"%@",request.URL.absoluteString);
NSString *url = request.URL.absoluteString;
NSRange range = [url rangeOfString:@"code="];
if (range.length != 0) {
NSInteger index = range.location + range.length;
NSString *code = [url substringFromIndex:index];
NSString *urlStr = @"https://api.weibo.com/oauth2/access_token";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *paramaDic = @{
@"client_id":@"500210150",
@"client_secret":@"9414dad23cbef8b45c2be459f5bbe4b3",
@"grant_type":@"authorization_code",
@"code":code,
@"redirect_uri":@"http://www.baidu.com"
};
[manager POST:url parameters:paramaDic success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
}
return YES;
}
@end
运行结果: