Facebook webView
@interfaceLoginViewController:UIViewController<UIWebViewDelegate>@property(nonatomic,retain)UIWebView*webview;@property(nonatomic,retain)NSString*accessToken;@property(nonatomic,retain)UIActivityIndicatorView*FbActive;@end@interfaceLoginViewController()@end@implementationLoginViewController@synthesizeaccessToken,webview,FbActive;-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview.//RemoveingtheUIWebviewCookiesNSHTTPCookie*cookie;NSHTTPCookieStorage*storage=[NSHTTPCookieStoragesharedHTTPCookieStorage];for(cookiein[storagecookies]){[storagedeleteCookie:cookie];}[[NSUserDefaultsstandardUserDefaults]synchronize];}-(IBAction)fbLoginPage:(UIButton*)sender1{NSString*facebookClientID=facebookAppId;NSString*redirectUri=@"http://www.facebook.com/connect/login_success.html";NSString*extended_permissions=@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins,email";NSString*url_string=[NSStringstringWithFormat:@"https://graph.facebook.com/oauth/authorize?client_id=%@&redirect_uri=%@&scope=%@&type=user_agent&display=touch",facebookClientID,redirectUri,extended_permissions];NSURL*url=[NSURLURLWithString:url_string];NSURLRequest*request=[NSURLRequestrequestWithURL:url];CGRectwebFrame=[self.viewframe];webFrame.origin.y=0;UIWebView*aWebView=[[UIWebViewalloc]initWithFrame:webFrame];[aWebViewsetDelegate:self];self.webview=aWebView;self.FbActive=[[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];self.FbActive.color=[UIColordarkGrayColor];self.FbActive.center=CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2);[self.FbActivestartAnimating];[webviewloadRequest:request];[self.webviewaddSubview:self.FbActive];[self.viewaddSubview:webview];}-(void)webViewDidFinishLoad:(UIWebView*)_webView{/***Sincethere'ssomeserversideredirectinginvolved,thismethod/functionwillbecalledseveraltimes*we'reonlyinterestedwhenweseeaurllike:http://www.facebook.com/connect/login_success.html#access_token=..........*///gettheurlstring[self.FbActivestopAnimating];NSString*url_string=[((_webView.request).URL)absoluteString];//lookingfor"access_token="NSRangeaccess_token_range=[url_stringrangeOfString:@"access_token="];//lookingfor"error_reason=user_denied"NSRangecancel_range=[url_stringrangeOfString:@"error_reason=user_denied"];//itexists?coolio,wehaveatoken,nowlet'sparseitout....if(access_token_range.length>0){//wewanteverythingafterthe'access_token='thusthepositionwhereitstarts+it'slengthintfrom_index=access_token_range.location+access_token_range.length;NSString*access_token=[url_stringsubstringFromIndex:from_index];//finallywehavetourldecodetheaccesstokenaccess_token=[access_tokenstringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//removeeverything'&'(inclusive)onward...NSRangeperiod_range=[access_tokenrangeOfString:@"&"];//movebeyondthe.access_token=[access_tokensubstringToIndex:period_range.location];//storeourrequesttoken....self.accessToken=access_token;//removeourwindow//UIWindow*window=[UIApplicationsharedApplication].keyWindow;//if(!window){//window=[[UIApplicationsharedApplication].windowsobjectAtIndex:0];//}[self.webviewremoveFromSuperview];self.webview=nil;//tellourcallbackfunctionthatwe'redoneloggingin:)//if((callbackObject!=nil)&&(callbackSelector!=nil)){//[callbackObjectperformSelector:callbackSelector];//}//theuserpressedcancel}elseif(cancel_range.length>0){//removeourwindow//UIWindow*window=[UIApplicationsharedApplication].keyWindow;//if(!window){//window=[[UIApplicationsharedApplication].windowsobjectAtIndex:0];//}[self.webviewremoveFromSuperview];self.webview=nil;//tellourcallbackfunctionthatwe'redoneloggingin:)//if((callbackObject!=nil)&&(callbackSelector!=nil)){//[callbackObjectperformSelector:callbackSelector];//}}[selfgetuserdetailes];}-(void)getuserdetailes{NSString*action=@"me";NSString*url_string=[NSStringstringWithFormat:@"https://graph.facebook.com/%@?",action];//tackonanygetvarswehave...NSDictionary*get_vars=nil;if((get_vars!=nil)&&([get_varscount]>0)){NSEnumerator*enumerator=[get_varskeyEnumerator];NSString*key;NSString*value;while((key=(NSString*)[enumeratornextObject])){value=(NSString*)[get_varsobjectForKey:key];url_string=[NSStringstringWithFormat:@"%@%@=%@&",url_string,key,value];}//endwhile}//endifif(accessToken!=nil){//nowthatanyvariableshavebeenappended,let'sattachtheaccesstoken....url_string=[NSStringstringWithFormat:@"%@access_token=%@",url_string,self.accessToken];url_string=[url_stringstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];NSLog(@"%@",url_string);NSURLRequest*request=[NSURLRequestrequestWithURL:[NSURLURLWithString:url_string]];NSError*err;NSURLResponse*resp;NSData*response=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:&resperror:&err];NSString*stringResponse=[[NSStringalloc]initWithData:responseencoding:NSUTF8StringEncoding];NSLog(@"%@",stringResponse);NSError*error;NSDictionary*FBResResjson=[NSJSONSerializationJSONObjectWithData:response//1options:kNilOptionserror:&error];NSLog(@"%@",FBResResjson);}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。