Final Effect Diagram:
Oauthviewcontroller.m
OAUTHVIEWCONTROLLER.M//20_ handsome no Weibo////Created by Beyond on 14-8-5. Copyright (c) 2014 Com.beyond.
All rights reserved. Authorization controller, run only once, obtain the current user's Access_token and UID, archive, switch the window's main controller #import "OauthViewController.h" @interface Oauthviewcontroller () <UIWebViewDelegate> {//member variable keep in mind that UIWebView *_webview is used in different methods @implementation Oaut
Hviewcontroller-(void) Loadview {//directly let WebView be the controller's view, avoid adding _webview again = [[UIWebView alloc]init];
Self.view = _webview;
}-(void) viewdidload {[Super viewdidload];
Set the agent to be the current controller to listen for the start load and end loading of the webview _webview.delegate = self; Application for certification address nsstring *oauthurl = [NSString stringwithformat:@ https://api.weibo.com/oauth2/authorize?client_id=%@
&response_type=code&redirect_uri=%@ ", Kappkey,kredirecturl];
Call the classification method, load the application authentication URL [_webview Loadurlstring:oauthurl]; #pragma mark-Proxy method//Start loading-(void) Webviewdidstartload: (UIWebView *) webview {log (@ "really starts loading--%@", WebView.request.UR
L); Show progress bar as soon as it starts loading
Mbprogresshud *hud = [Mbprogresshud showhudaddedto:webview animated:yes];
Hud.labeltext = @ "page load ..."; //Whether to start loading a page-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype
:(Uiwebviewnavigationtype) Navigationtype {log (@ "Can load--%@", WebView.request.URL);
return YES;
}//Page load complete-(void) Webviewdidfinishload: (UIWebView *) webview {log (@ "Loaded--%@", WebView.request.URL);
Once the load is complete, hide the progress bar [Mbprogresshud Hideallhudsforview:webview Animated:yes]; After the user agrees to authorize, the returned URL contains the authorized Request_code, in the form of: http://www.abc.com/?code=888888888888//returned to the user-authorized Request_code page, Need to intercept code, and then continue to splice the URL, the launch of the 3rd request (this time must be post), the final return required Access_token nsstring *redirecturlcontainscode = _
webView.request.URL.absoluteString;
Classification method, starting with the last side of the tag string on the left, intercepting the remaining strings nsstring *code = [Redirecturlcontainscode substrfromleftflagstr:@ "code="];
If it is not a URL that returns code, do nothing if (code = = nil) return; Now ready to launch the last request, splicing the 3rd request to the required URL, this request returned Dongdong, will be the most important user of the Accesstoken, also containsThe user's uid nsstring *accesstokenrequesturlstr = [NSString stringwithformat:@ https://api.weibo.com/oauth2/access_token ? client_id=%@&client_secret=%@&grant_type=authorization_code&redirect_uri=%@&code=%@ ", KAppKey,
Kappsecret,kredirecturl,code];
1, create URL nsurl *accesstokenrequesturl = [Nsurl urlwithstring:accesstokenrequesturlstr]; 2, create POST request nsmutableurlrequest *mutrequest = [[Nsmutableurlrequest alloc]initwithurl:accesstokenrequesturl
Cachepolicy:nsurlrequestuseprotocolcachepolicy Timeoutinterval:10];
Set the request mode to post, default to get [Mutrequest sethttpmethod:@ "POST"]; 3, connect the server and receive the returned data nsdata *receiveddata = [Nsurlconnection sendsynchronousrequest:mutrequest returningresponse:nil
Error:nil]; Converts the data returned by the server into a string (essentially JSON data) nsstring *RESPONSESTR = [[NSString alloc]initwithdata:receiveddata encoding:
Nsutf8stringencoding];
Log (@ "Response json is:%@", RESPONSESTR); 4, from RESPONSESTR (essentially JSON data) get to Access_token//CONVERT (JSON data) into a dictionary first nsdictionary*dictionary = [Nsjsonserialization jsonobjectwithdata:receiveddata options:nsjsonreadingmutablecontainers Error:nil
];
Through the key, take to access_token nsstring *access_token = [Dictionary objectforkey:@ "Access_token"];
Log (@ "access token is:%@", access_token);
Through the key, take to the user's uid nsstring *uid = [Dictionary objectforkey:@ "UID"];
Log (@ "UID is:%@", UID); Authorization succeeded, switching root controller to host controller uiactionsheet *actionsheet = [[Uiactionsheet alloc]initwithtitle:@ ' authorized success ' Delegate:nil
cancelbuttontitle:@ "Cancel" destructivebuttontitle:@ "determine" otherbuttontitles:nil];
[Actionsheet ShowInView:self.view.window];
} @end
Supplementary Note:
No. 0 Step,
Register as a developer, verify the mailbox, you can create mobile applications,
Note the Appkey and Appsecretthat the system automatically generates for the application.
And in the advanced information of the application information, set the address of the callback page of the authorization completion Redirect_uri
Since this is a mobile client, not a Web application,
So when you create an application,Redirect_uri can write casually,
But you must use the same address globally Redirect_uri
1th Step,
To apply for an unauthorized Request_code,
The essence is to come to Weibo login page , that is, _webview the first loaded URL
The address format is as follows:
https://api.weibo.com/oauth2/authorize?client_id=APPKEY&response_type=code&redirect_uri=https:// Api.weibo.com/oauth2/default.html
Appkey is the only application ID that is automatically generated by the system when the application is created
Redirect_uri, must be consistent with the creation of the application when you fill in
2nd step,
After the user entered the account number and password, click Login,
The page will automatically go to the licensing page,
If the user clicks on the Authorization button, the page will redirect to http://redirectURL/?code=888888888888,
The job to do is to intercept the code value in the redirected URL ( each time it is different),
This code is actually an authorized Request_code,
But it's just a middleman and can't use it to get user information
The address format is as follows:
Https://api.weibo.com/oauth2/default.html?code=fa4efb6310411f948423e69adeabec08
3rd Step,
With the code intercepted in step 2nd, assemble the URL again,
Initiates the last request (must be a POST request),
At this point, the data returned by the server is a required JSON data.
It contains three key value pairs
{"
Access_token": "This is the real Access_token",
"remind_in": "157679999",
"expires_in": 157679999,
"UID" : "UID of the authorized user"
}