From 57818a0c0e1e7b24dfd849dc7664311ea78db509 Mon Sep 17 00:00:00 2001 From: Yujuan Bao Date: Wed, 1 Sep 2010 11:47:48 -0700 Subject: [PATCH] iOS SDK:Facebook delegate fbDidNotLogin add a parameter to check if user cancel and address several other issue --- .../DemoApp/Classes/DemoAppViewController.m | 6 ++--- .../theRunAround/Classes/mainViewController.m | 2 +- src/FBLoginDialog.h | 2 +- src/FBLoginDialog.m | 10 ++++---- src/FBRequest.h | 6 +++++ src/FBRequest.m | 23 ++++++++++++++----- src/Facebook.h | 2 +- src/Facebook.m | 8 +++---- 8 files changed, 38 insertions(+), 21 deletions(-) diff --git a/sample/DemoApp/Classes/DemoAppViewController.m b/sample/DemoApp/Classes/DemoAppViewController.m index ff4136fd10..04baf5fce4 100644 --- a/sample/DemoApp/Classes/DemoAppViewController.m +++ b/sample/DemoApp/Classes/DemoAppViewController.m @@ -45,7 +45,7 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil * Set initial view */ - (void) viewDidLoad { - _facebook = [[[[Facebook alloc] init] autorelease] retain]; + _facebook = [[Facebook alloc] init]; [self.label setText:@"Please log in"]; _getUserInfoButton.hidden = YES; _getPublicInfoButton.hidden = YES; @@ -200,7 +200,7 @@ -(void) fbDidLogin { /** * Callback for facebook did not login */ -- (void)fbDidNotLogin { +- (void)fbDidNotLogin:(BOOL)cancelled { NSLog(@"did not login"); } @@ -240,7 +240,7 @@ - (void)request:(FBRequest*)request didFailWithError:(NSError*)error{ * The resulting object may be a dictionary, an array, a string, or a number, depending * on thee format of the API response. */ -- (void)request:(FBRequest*)request didLoad:(id)result{ +- (void)request:(FBRequest*)request didLoad:(id)result { if ([result isKindOfClass:[NSArray class]]) { result = [result objectAtIndex:0]; } diff --git a/sample/theRunAround/Classes/mainViewController.m b/sample/theRunAround/Classes/mainViewController.m index 495c19b10d..d66cd966f1 100644 --- a/sample/theRunAround/Classes/mainViewController.m +++ b/sample/theRunAround/Classes/mainViewController.m @@ -74,7 +74,7 @@ - (void)viewDidLoad { _session = [[Session alloc] init]; _facebook = [[_session restore] retain]; if (_facebook == nil) { - _facebook = [[[[Facebook alloc] init] autorelease] retain]; + _facebook = [[Facebook alloc] init]; _fbButton.isLoggedIn = NO; _addRunButton.hidden = YES; [self.view addSubview:self.logoutView]; diff --git a/src/FBLoginDialog.h b/src/FBLoginDialog.h index 2056f07531..0194d14c50 100644 --- a/src/FBLoginDialog.h +++ b/src/FBLoginDialog.h @@ -41,7 +41,7 @@ - (void) fbDialogLogin:(NSString *) token expirationDate:(NSDate *) expirationDate; -- (void) fbDialogNotLogin; +- (void) fbDialogNotLogin:(BOOL) cancelled; @end diff --git a/src/FBLoginDialog.m b/src/FBLoginDialog.m index d2f969bdf7..70552bf8ec 100644 --- a/src/FBLoginDialog.m +++ b/src/FBLoginDialog.m @@ -59,7 +59,7 @@ - (void) dialogDidSucceed:(NSURL*)url { } } - if ((token == (NSString *) [NSNull null]) || (token.length ==0)) { + if ((token == (NSString *) [NSNull null]) || (token.length == 0)) { [self dialogDidCancel:url]; [self dismissWithSuccess:NO animated:YES]; } else { @@ -76,8 +76,8 @@ - (void) dialogDidSucceed:(NSURL*)url { */ - (void)dialogDidCancel:(NSURL *)url { [self dismissWithSuccess:NO animated:YES]; - if ([_loginDelegate respondsToSelector:@selector(fbDialogNotLogin)]) { - [_loginDelegate fbDialogNotLogin]; + if ([_loginDelegate respondsToSelector:@selector(fbDialogNotLogin:)]) { + [_loginDelegate fbDialogNotLogin:YES]; } } @@ -85,8 +85,8 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { if (!(([error.domain isEqualToString:@"NSURLErrorDomain"] && error.code == -999) || ([error.domain isEqualToString:@"WebKitErrorDomain"] && error.code == 102))) { [super webView:webView didFailLoadWithError:error]; - if ([_loginDelegate respondsToSelector:@selector(fbDialogNotLogin)]) { - [_loginDelegate fbDialogNotLogin]; + if ([_loginDelegate respondsToSelector:@selector(fbDialogNotLogin:)]) { + [_loginDelegate fbDialogNotLogin:NO]; } } } diff --git a/src/FBRequest.h b/src/FBRequest.h index f24587c1c5..29d8119edf 100644 --- a/src/FBRequest.h +++ b/src/FBRequest.h @@ -101,6 +101,12 @@ */ - (void)request:(FBRequest*)request didLoad:(id)result; +/** + * Called when a request returns a response. + * + * The result object is the raw response from the server of type NSData + */ +- (void)request:(FBRequest*)request didLoadRawResponse:(NSData*)data; @end diff --git a/src/FBRequest.m b/src/FBRequest.m index a2dafedb5e..0ff85f12da 100644 --- a/src/FBRequest.m +++ b/src/FBRequest.m @@ -223,15 +223,26 @@ - (void)failWithError:(NSError*)error { * private helper function: handle the response data */ - (void)handleResponseData:(NSData*)data { - NSError* error = nil; - id result = [self parseJsonResponse:data error:&error]; - if (error) { - [self failWithError:error]; - } else if ([_delegate respondsToSelector:@selector(request:didLoad:)]) { - [_delegate request:self didLoad:result]; + if ([_delegate respondsToSelector:@selector(request:didLoadRawResponse:)]) { + [_delegate request:self didLoadRawResponse:data]; } + + if ([_delegate respondsToSelector:@selector(request:didLoad:)] || + [_delegate respondsToSelector:@selector(request:didFailWithError:)]) { + NSError* error = nil; + id result = [self parseJsonResponse:data error:&error]; + if (error) { + [self failWithError:error]; + } else if ([_delegate respondsToSelector:@selector(request:didLoad:)]) { + [_delegate request:self didLoad:(result == nil ? data : result)]; + } + + } + } + + ////////////////////////////////////////////////////////////////////////////////////////////////// // public diff --git a/src/Facebook.h b/src/Facebook.h index e6baaa6085..a1d1ea0cd1 100644 --- a/src/Facebook.h +++ b/src/Facebook.h @@ -96,7 +96,7 @@ /** * Called when the user dismiss the dialog without login */ -- (void)fbDidNotLogin; +- (void)fbDidNotLogin:(BOOL)cancelled; /** * Called when the user is logged out diff --git a/src/Facebook.m b/src/Facebook.m index 8046f05a0c..458b74cd4f 100644 --- a/src/Facebook.m +++ b/src/Facebook.m @@ -411,9 +411,9 @@ - (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate /** * Did not login call the not login delegate */ -- (void) fbDialogNotLogin { - if ([self.sessionDelegate respondsToSelector:@selector(fbDidNotLogin)]) { - [_sessionDelegate fbDidNotLogin]; +- (void) fbDialogNotLogin:(BOOL)cancelled { + if ([self.sessionDelegate respondsToSelector:@selector(fbDidNotLogin:)]) { + [_sessionDelegate fbDidNotLogin:cancelled]; } } @@ -425,7 +425,7 @@ - (void) fbDialogNotLogin { * Handle the auth.ExpireSession api call failure */ - (void)request:(FBRequest*)request didFailWithError:(NSError*)error{ - NSLog(@"Failed to expiration the session"); + NSLog(@"Failed to expire the session"); } ///////////////////////////////////////////////////////////////////////////////////////////////////