@interface LoginViewController : UIViewController<UIWebViewDelegate>@property(nonatomic,retain)UIWebView *webview;@property (nonatomic, retain) NSString *accessToken;@property(nonatomic,retain)UIActivityIndicatorView *FbActive;@[email protected] LoginViewController ()@[email protected] [email protected] accessToken,webview,FbActive;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. //Removeing the UIWebview Cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [storage cookies]) { [storage deleteCookie:cookie]; } [[NSUserDefaults standardUserDefaults] synchronize];}-(IBAction)fbLoginPage:(UIButton *)sender1{ NSString *facebookClientID =facebookAppId; NSString *redirectUri = @"http://www.facebook.com/connect/login_success.html"; NSString *[email protected]"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins,email"; NSString *url_string = [NSString stringWithFormat:@"https://graph.facebook.com/oauth/authorize?client_id=%@&redirect_uri=%@&scope=%@&type=user_agent&display=touch", facebookClientID, redirectUri, extended_permissions]; NSURL *url = [NSURL URLWithString:url_string]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; CGRect webFrame =[self.view frame]; webFrame.origin.y = 0; UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame]; [aWebView setDelegate:self]; self.webview = aWebView; self.FbActive = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; self.FbActive.color=[UIColor darkGrayColor]; self.FbActive.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2); [self.FbActive startAnimating]; [webview loadRequest:request]; [self.webview addSubview:self.FbActive]; [self.view addSubview:webview];}- (void)webViewDidFinishLoad:(UIWebView *)_webView { /** * Since there‘s some server side redirecting involved, this method/function will be called several times * we‘re only interested when we see a url like: http://www.facebook.com/connect/login_success.html#access_token=.......... */ //get the url string [self.FbActive stopAnimating]; NSString *url_string = [((_webView.request).URL) absoluteString]; //looking for "access_token=" NSRange access_token_range = [url_string rangeOfString:@"access_token="]; //looking for "error_reason=user_denied" NSRange cancel_range = [url_string rangeOfString:@"error_reason=user_denied"]; //it exists? coolio, we have a token, now let‘s parse it out.... if (access_token_range.length > 0) { //we want everything after the ‘access_token=‘ thus the position where it starts + it‘s length int from_index = access_token_range.location + access_token_range.length; NSString *access_token = [url_string substringFromIndex:from_index]; //finally we have to url decode the access token access_token = [access_token stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //remove everything ‘&‘ (inclusive) onward... NSRange period_range = [access_token rangeOfString:@"&"]; //move beyond the . access_token = [access_token substringToIndex:period_range.location]; //store our request token.... self.accessToken = access_token; //remove our window// UIWindow* window = [UIApplication sharedApplication].keyWindow;// if (!window) {// window = [[UIApplication sharedApplication].windows objectAtIndex:0];// } [self.webview removeFromSuperview]; self.webview=nil; //tell our callback function that we‘re done logging in :) // if ( (callbackObject != nil) && (callbackSelector != nil) ) { // [callbackObject performSelector:callbackSelector]; // } //the user pressed cancel } else if (cancel_range.length > 0) { //remove our window// UIWindow* window = [UIApplication sharedApplication].keyWindow;// if (!window) {// window = [[UIApplication sharedApplication].windows objectAtIndex:0];// } [self.webview removeFromSuperview]; self.webview=nil; //tell our callback function that we‘re done logging in :) // if ( (callbackObject != nil) && (callbackSelector != nil) ) { // [callbackObject performSelector:callbackSelector]; // } } [self getuserdetailes];}-(void)getuserdetailes{ NSString *[email protected]"me"; NSString *url_string = [NSString stringWithFormat:@"https://graph.facebook.com/%@?", action]; //tack on any get vars we have... NSDictionary *get_vars=nil; if ( (get_vars != nil) && ([get_vars count] > 0) ) { NSEnumerator *enumerator = [get_vars keyEnumerator]; NSString *key; NSString *value; while ((key = (NSString *)[enumerator nextObject])) { value = (NSString *)[get_vars objectForKey:key]; url_string = [NSString stringWithFormat:@"%@%@=%@&", url_string, key, value]; }//end while }//end if if (accessToken != nil) { //now that any variables have been appended, let‘s attach the access token.... url_string = [NSString stringWithFormat:@"%@access_token=%@", url_string, self.accessToken]; url_string = [url_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%@",url_string); NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url_string]]; NSError *err; NSURLResponse *resp; NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&err]; NSString *stringResponse = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSLog(@"%@",stringResponse); NSError* error; NSDictionary *FBResResjson = [NSJSONSerialization JSONObjectWithData:response//1 options:kNilOptions error:&error]; NSLog(@"%@",FBResResjson); }}
时间: 2024-10-11 15:16:06