連接微博

鏈接?

1.進(jìn)入微博開放平臺的微連接,創(chuàng)建應(yīng)用

2.獲取App Key:App Secret:

3.設(shè)置授權(quán)回調(diào)頁:取消授權(quán)回調(diào)頁:


代碼實現(xiàn)流程:

1.創(chuàng)建webView,設(shè)置代理,實現(xiàn)協(xié)議方法

2.首先訪問授權(quán)接口Oauth2/authorize接口

https://api.weibo.com/oauth2/authorize?client_id=App Key&redirect_uri=回調(diào)頁面的網(wǎng)址&display=mobile

3.構(gòu)建請求

webView加載

_webView= [[UIWebViewalloc]initWithFrame:self.view.bounds];

_webView.delegate=self;

[self.view addSubview:_webView];

NSURL*url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@&display=mobile",kAPPKey,kRedirect_uri]];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

[_webView loadRequest:request];

4.用戶登錄

在協(xié)議方法將要開始加載一個請求時

返回一個授權(quán)連接點擊授權(quán)

授權(quán)服務(wù)器返回一個連接連接中包含(redirect_uri+code)

輸出:http://www.new.com/?code=869f51c8f3dfd223824606808059fa50

請求授權(quán)完成后執(zhí)行shouldStartLoadWithRequest判斷是否返回code

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

NSLog(@"即將加載的請求路徑-- %@",request.URL.absoluteURL);

NSString *str = request.URL.absoluteString;

NSRange range = [str rangeOfString:@"?code"];

//發(fā)現(xiàn)str中有沒有code

if(range.location!=NSNotFound) {

//獲取到code

//拆分找code的碼

NSArray *array = [str componentsSeparatedByString:@"="];

NSString *code =[array lastObject];

NSLog(@"%@",code);

//創(chuàng)建請求管理類

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

NSString *urlStr =@"https://api.weibo.com/oauth2/access_token";

NSDictionary *dic = @{@"client_id":kAPPKey, @"client_secret":kAPPSecret, @"grant_type":@"authorization_code", @"code":code, @"redirect_uri":kRedirect_uri};

manager.responseSerializer.acceptableContentTypes= [NSSet setWithObject:@"text/plain"];

[manager POST:urlStrparameters:dicconstructingBodyWithBlock:^(id_NonnullformData) {

}progress:^(NSProgress*_NonnulluploadProgress) {

}success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

NSLog(@"responseObject ---> %@",responseObject);

[NSUserDefaults standardUserDefaults] setObject:<#(nullable id)#> forKey:<#(nonnull NSString *)#>

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

NSLog(@"error -- %@",error);

}];

}

returnYES;

根據(jù)code構(gòu)建一次請求

請求結(jié)束方法中獲取access token


#import"ViewController.h"

#import"AFNetworking.h"

#define kAPPKey @"0000000000"

#define kAPP_Secret @"be7b7d29791adeca9000c7a174580d3e"

#define kRedirect_uri @"http://baidu.com"

@interfaceViewController(){

UIWebView *_webView;

}

@end

@implementationViewController

- (void)viewDidLoad {

[super viewDidLoad];

_webView= [[UIWebView alloc] initWithFrame:self.view.frame];

_webView.delegate = self;

[self.view addSubview:_webView];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@&display=mobile",kAPPKey,kRedirect_uri]]];

[_webView loadRequest:request];

/*

NSString *str =@"abcdefghijklmnopqrstuvwxyz";

//范圍:o后面的部分

NSRange range = [strrangeOfString:@"o"];

NSString *last = [NSString stringWithFormat:@"%ld",range.location];

NSLog(@"%@",last);//14

//拆分

NSArray *arr = [str componentsSeparatedByString:@"o"];

NSLog(@"%@",[arr lastObject]);//pqrstuvwxyz

NSLog(@"%@",[arr firstObject]);//abcdefghijklmn

*/

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

NSLog(@"---->%@",request.URL.absoluteURL);

NSString *str = request.URL.absoluteString;

NSRange range = [str rangeOfString:@"?code"];

//?code后面的部分存在的話,就進(jìn)入下面的方法

if(range.location != NSNotFound) {

//停止加載不用進(jìn)入回調(diào)頁面

[_webView stopLoading];

//拆分

NSArray *array = [str componentsSeparatedByString:@"="];

//拿出=后面的部分

NSString *code = [array lastObject];

//請求路徑

NSString *url =@"https://api.weibo.com/oauth2/access_token";

//封裝參數(shù)

NSDictionary *parma = @{@"client_id":kAPPKey, @"client_secret":kAPP_Secret, @"grant_type":@"authorization_code", @"code":code, @"redirect_uri":kRedirect_uri};

//獲得網(wǎng)絡(luò)管理

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

//設(shè)置適合AFNetWorking的響應(yīng)頭

manager.responseSerializer.acceptableContentTypes= [NSSet setWithObject:@"text/plain"];

//連接

[manager POST:url parameters:parma progress:^(NSProgress *_NonnulluploadProgress) {

}success:^(NSURLSessionDataTask *_Nonnulltask,id_NullableresponseObject) {

NSLog(@"responseObject ---> %@",responseObject);

// ???????????responseObject ---> {

// ???????????????"access_token" = "2.00hMyWODWC__mD6a71a50f91CfpNwB";

// ???????????????"expires_in" = 157670000;

// ???????????????"remind_in" = 157670000;

// ???????????????uid = 2963125123;

// ???????????}

//本地數(shù)據(jù)持久化存儲數(shù)據(jù):方便后面取出使用

//1.屬性列表的方式plist文件輕量級的數(shù)據(jù)涉及到的主要的類NSUserDefaultsv

[[NSUserDefaults standardUserDefaults] setObject:[response ObjectobjectForKey:@"access_token"] forKey:@"access_token"];

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

NSLog(@"error --- %@",error);

}];

}

return YES;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • AFNNetworking 2.0你相信你一定知道AFNNetworking,不知道你還可以看看該作者的博文,所以...
    瞎嘚嘚閱讀 766評論 1 1
  • 1.在開發(fā)的時候可以創(chuàng)建一個工具類,繼承自我們的AFN中的請求管理者,再控制器中真正發(fā)請求的代碼使用自己封裝的工具...
    紅樓那一場夢閱讀 3,627評論 2 3
  • 218.241.181.202 wxhl60 123456 192.168.10.253 wxhl66 wxhl6...
    CYC666閱讀 1,553評論 0 6
  • 1.不可變數(shù)組轉(zhuǎn)變?yōu)榭勺償?shù)組聲明實例變量的數(shù)組 必須記得實現(xiàn) 對于遍歷數(shù)組找到對象后 如果還需要查找 記得先結(jié)束 ...
    小新xin閱讀 1,032評論 0 1
  • http://www.cnblogs.com/mddblog/p/5281748.html 一、整體介紹 UIWe...
    F麥子閱讀 1,328評論 0 2

友情鏈接更多精彩內(nèi)容