#import "ViewController.h "
#import "AFNetworking.h"@interfaceViewcontroller () <UIWebViewDelegate>@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //1. Using WebViewUIWebView *webview =[[UIWebView alloc] initWithFrame:self.view.bounds]; [Self.view Addsubview:webview]; //for WebView to initiate a request I need a Sina login page//Interfacehttps://api.weibo.com/oauth2/authorizethis interface. //using that platform, which platform to use the SDK will only need to provide three parameters app key, App Sercect, callback page URL. /** client_id assigned appkey when applying for application. Redirect_uri Authorization callback address, the external application needs to be the same as the set callback address, the site application needs to fill in the address of the canvas page. */Nsurl*url = [Nsurl urlwithstring:@"https://api.weibo.com/oauth2/authorize?client_id=2320601559&redirect_uri=http://www.baidu.com"]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; WebView.Delegate=Self ; [WebView Loadrequest:request]; //when the user clicks on the authorization, the Sina server returns a redirect address, and binds an important parameter code//What does code do? The Demo app will find this code and use this code to go to the Sina server to request Toke .}- (void) Webviewdidstartload: (UIWebView *) WebView {nsstring*url =webView.request.URL.absoluteString; NSLog (@"webview start loading, load address is:%@", URL);}- (void) Webviewdidfinishload: (UIWebView *) WebView {NSLog (@"WebView Loading End");}-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype {//determine if the request is a redirect request returned by the server?NSString *url =request. url.absolutestring; //whether it contains code=Nsrange range = [url rangeofstring:@"code="]; if(Range.length! =0) { //This will intercept the data behind the code=.// http://www.baidu.com/?code=29a7addea0226eeaf94768440639189fNsinteger index = range.location +range.length; NSString*code =[url substringfromindex:index]; //initiate a request to get Access_token /** client_id string is assigned when applying the Appkey. Client_secret string The Appsecret that is assigned when the application is applied. Grant_type type of string request, fill in the required type and range Authorization_code Grant_type to Authorization_code Describes the code string that is obtained by calling authorize. The Redirect_uri string callback address needs to match the callback address in the registration application. */Afhttpsessionmanager*sessionmanager =[Afhttpsessionmanager Manager]; NSString*tokenurl =@"Https://api.weibo.com/oauth2/access_token"; Nsdictionary*params= @{ @"client_id":@"2320601559", @"Client_secret":@"d77590e7cb2b4dda50e5c17359380d0e", @"Grant_type":@"Authorization_code", @"Code": Code,@"Redirect_uri":@"http://www.baidu.com" }; [SessionManager post:tokenurl parameters:paramssuccess:^ (Nsurlsessiondatatask *task,IDresponseobject) {NSLog (@"Token request succeeded%@", responseobject);//2.00ae6vvd6tadxcd89cbb1e870cqjn_nsdictionary*result = (Nsdictionary *) Responseobject; NSString*token = result[@"Access_token"]; [SessionManager GET:@"Https://api.weibo.com/2/statuses/public_timeline.json"parameters:@{@"Access_token": token} success:^ (Nsurlsessiondatatask *task,IDresponseobject) {NSLog (@"The address successfully returned is:%@", Responseobject); } Failure:^ (Nsurlsessiondatatask *task, Nserror *error) {NSLog (@"failed to return."); }]; //2.00ae6vvd6tadxcd89cbb1e870cqjn_} failure:^ (Nsurlsessiondatatask *task, Nserror *error) {NSLog (@"token request failed."); }]; //prevents the current webviwe from jumping to the callback page. returnNO; } returnYES;}@end
iOS Sina Weibo OAuth2.0 authentication code