#import "ViewController.h"#defineKappkey @ "706003107"#defineKappsecret @ "53b3f1774f9a050e1d4931bab3b55633"#defineKredirect_url @ "Www.baidu.com"#defineKaccesstokenkey @ "Kaccesstokenkey"#defineKexpirestimekey @ "Kexpirestimekey"#defineKuseridkey @ "Kuseridkey"@interfaceViewcontroller () <UIWebViewDelegate>@property (nonatomic,strong) UIWebView*WebView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; }-(Ibaction) logindidclicked: (ID) Sender {//use OAuth's oauth2 to initiate requests, splice URLs, and parameters to start with? ,& connect the different parameters,NSString *urlstring = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@", Kappkey,kredirect_url]; Nsurl*url =[Nsurl urlwithstring:urlstring]; //Send request-create session//nsurlsession *session = [Nsurlsession sharedsession]; //Create a request, Web pageNsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy: Nsurlrequestreloadignoringlocalandremotecachedata timeOutInterval: -]; //loading pages with UIWebViewSelf.webview =[[UIWebView alloc]initwithframe:self.view.bounds]; _webview.Delegate=Self ; [Self.view Addsubview:_webview]; [_webview loadrequest:request];}-(Ibaction) Logout: (ID) Sender {nsuserdefaults*usedefaults =[Nsuserdefaults Standarduserdefaults]; [Usedefaults Setobject:nil Forkey:kaccesstokenkey]; [Usedefaults Setobject:nil Forkey:kexpirestimekey]; [Usedefaults Setobject:nil Forkey:kuseridkey]; [Usedefaults synchronize];}#pragmaMark----------------(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *Request Navigationtype: (uiwebviewnavigationtype) navigationtype{//The target obtains the code's Status Code solution, resolves the URL of the request that will be loaded, and once the URL contains code=, stopNsrange range = [[request]. URL relativestring]rangeofstring:@"code="]; if(range.length!=0) { //Note that the requested request now contains code, parsing the content behind code ,NSString *code = [[Request]. URL relativestring]substringfromindex:range.location+Range.length]; //Erase the interface WebView[Self.webview Removefromsuperview]; //use code to get a accesstoken, solution, to send a POST request to the serverNsurl *url = [Nsurl urlwithstring:@"Https://api.weibo.com/oauth2/access_token"]; //to create a mutable requestNsmutableurlrequest *uprequest =[Nsmutableurlrequest Requestwithurl:url]; //set the request in the same way as Post[Uprequest Sethttpmethod:@"POST"]; //Create sessionNsurlsession *session =[Nsurlsession sharedsession]; //prepare the uploaded data and stitch the uploaded parameters into a stringNSString *datastring = [NSString stringWithFormat:@"client_id=%@&client_secret=%@&grant_type=authorization_code&code=%@&redirect_uri=%@", Kappkey,kappsecret,code,kredirect_url]; //Convert a string to NSDataNSData *updata =[datastring datausingencoding:nsutf8stringencoding]; //Create a UploadtaskNsurlsessionuploadtask *uploadtask = [Session uploadtaskwithrequest:uprequest Fromdata:updata completionHandler:^ ( NSData * _nullable data, Nsurlresponse * _nullable response, Nserror *_nullable Error) { //NSData converted to nsdictionaryNsdictionary *resultdic =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; NSLog (@"%@", Resultdic); //Save access_token,expires_in UIDNSString *accesstoken = [Resultdic objectforkey:@"Access_token"]; NSString*expiresin = [Resultdic objectforkey:@"expires_in"]; NSString*uid = [Resultdic objectforkey:@"UID"]; Nsuserdefaults*usedefaults =[Nsuserdefaults Standarduserdefaults]; [Usedefaults Setobject:accesstoken Forkey:kaccesstokenkey]; [Usedefaults Setobject:expiresin Forkey:kexpirestimekey]; [Usedefaults Setobject:uid Forkey:kuseridkey]; [Usedefaults Synchronize]; }]; [Uploadtask resume]; returnNO; }Else{ returnYES; }}@end
Weibo API calls