From 1b5c17e9d15593b1aea31df06f3e3778c2cbeb37 Mon Sep 17 00:00:00 2001 From: Dave Johnson Date: Mon, 22 Aug 2011 14:15:37 -0700 Subject: [PATCH] Add support for setting the uid in the session for ios --- .../Plugins/FBPlugin/FacebookConnectPlugin.m | 71 +++++------------ example/iOS/www/index.html | 3 - native/ios/FacebookConnectPlugin.m | 77 ++++++------------- www/pg-plugin-fb-connect.js | 6 ++ 4 files changed, 47 insertions(+), 110 deletions(-) diff --git a/example/iOS/PhoneGapFacebookConnect/Plugins/FBPlugin/FacebookConnectPlugin.m b/example/iOS/PhoneGapFacebookConnect/Plugins/FBPlugin/FacebookConnectPlugin.m index 652377243..1fcdccf5f 100755 --- a/example/iOS/PhoneGapFacebookConnect/Plugins/FBPlugin/FacebookConnectPlugin.m +++ b/example/iOS/PhoneGapFacebookConnect/Plugins/FBPlugin/FacebookConnectPlugin.m @@ -31,20 +31,8 @@ - (void) handleOpenURL:(NSNotification*)notification BOOL ok = [facebook handleOpenURL:url]; if (ok) { - - - NSDictionary* session = [NSDictionary - dictionaryWithObjects:[NSArray arrayWithObjects:self.facebook.accessToken, [self.facebook.expirationDate description], APP_SECRET, [NSNumber numberWithBool:YES], @"...", @"...", nil] - forKeys:[NSArray arrayWithObjects:@"access_token", @"expires", @"secret", @"session_key", @"sig", @"uid", nil]]; - NSDictionary* status = [NSDictionary - dictionaryWithObjects:[NSArray arrayWithObjects:@"connected", session, nil] - forKeys:[NSArray arrayWithObjects:@"status", @"session", nil]]; - - - PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsDictionary:status]; - NSString* callback = [result toSuccessCallbackString:self.loginCallbackId]; - // we need to wrap the callback in a setTimeout(func, 0) so it doesn't block the UI (handleOpenURL limitation) - [super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]]; + //get the UID of the currently logged in user + [facebook requestWithGraphPath:@"me" andDelegate:self]; } } @@ -131,36 +119,6 @@ - (void) dealloc [super dealloc]; } -/** - * Called when the user successfully logged in. - */ -- (void)fbDidLogin -{ - // [facebook dialog:@"feed" andDelegate:self]; - //[facebook requestWithGraphPath:@"me/friends" andDelegate:self]; - NSString* jsResult = [NSString stringWithFormat:@"FacebookGap.onLogin();"]; - [self.webView stringByEvaluatingJavaScriptFromString:jsResult]; - -} - -/** - * Called when the user dismissed the dialog without logging in. - */ -- (void)fbDialogNotLogin:(BOOL)cancelled -{ - // this NEVER seems to happen - NSString* jsResult = [NSString stringWithFormat:@"FacebookGap.onDidNotLogin(%d);",cancelled]; - [self.webView stringByEvaluatingJavaScriptFromString:jsResult]; -} - -/** - * Called when the user logged out. - */ -- (void)fbDidLogout -{ - NSString* jsResult = [NSString stringWithFormat:@"FacebookGap.onLogout();"]; - [self.webView stringByEvaluatingJavaScriptFromString:jsResult]; -} //////////////////////////////////////////////////////////////////// // FBRequestDelegate @@ -191,15 +149,24 @@ - (void)request:(FBRequest *)request didFailWithError:(NSError *)error /** * Called when a request returns and its response has been parsed into - * an object. + * an object. This is called only by the Graph API call to get the UID * * 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 -{ - NSString* jsResult = [NSString stringWithFormat:@"fbRequestResult(%@);", [result JSONRepresentation]]; - [self.webView stringByEvaluatingJavaScriptFromString:jsResult]; +{ + NSDictionary* session = [NSDictionary + dictionaryWithObjects:[NSArray arrayWithObjects:self.facebook.accessToken, [self.facebook.expirationDate description], APP_SECRET, [NSNumber numberWithBool:YES], @"...", [result valueForKey:@"id"], nil] + forKeys:[NSArray arrayWithObjects:@"access_token", @"expires", @"secret", @"session_key", @"sig", @"uid", nil]]; + NSDictionary* status = [NSDictionary + dictionaryWithObjects:[NSArray arrayWithObjects:@"connected", session, nil] + forKeys:[NSArray arrayWithObjects:@"status", @"session", nil]]; + + PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsDictionary:status]; + NSString* callback = [pluginResult toSuccessCallbackString:self.loginCallbackId]; + // we need to wrap the callback in a setTimeout(func, 0) so it doesn't block the UI (handleOpenURL limitation) + [super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]]; } ///** @@ -222,7 +189,7 @@ - (void)request:(FBRequest *)request didLoad:(id)result */ - (void)dialogDidComplete:(FBDialog *)dialog { - + // TODO } /** @@ -230,7 +197,7 @@ - (void)dialogDidComplete:(FBDialog *)dialog */ - (void)dialogCompleteWithUrl:(NSURL *)url { - + // TODO } /** @@ -238,7 +205,7 @@ - (void)dialogCompleteWithUrl:(NSURL *)url */ - (void)dialogDidNotCompleteWithUrl:(NSURL *)url { - + // TODO } /** @@ -246,7 +213,7 @@ - (void)dialogDidNotCompleteWithUrl:(NSURL *)url */ - (void)dialogDidNotComplete:(FBDialog *)dialog { - + // TODO } /** diff --git a/example/iOS/www/index.html b/example/iOS/www/index.html index 895056abe..3a28fece3 100755 --- a/example/iOS/www/index.html +++ b/example/iOS/www/index.html @@ -9,9 +9,6 @@
- - -
diff --git a/native/ios/FacebookConnectPlugin.m b/native/ios/FacebookConnectPlugin.m index 003718b10..1fcdccf5f 100755 --- a/native/ios/FacebookConnectPlugin.m +++ b/native/ios/FacebookConnectPlugin.m @@ -31,20 +31,8 @@ - (void) handleOpenURL:(NSNotification*)notification BOOL ok = [facebook handleOpenURL:url]; if (ok) { - - - NSDictionary* session = [NSDictionary - dictionaryWithObjects:[NSArray arrayWithObjects:self.facebook.accessToken, [self.facebook.expirationDate description], APP_SECRET, [NSNumber numberWithBool:YES], @"...", @"...", nil] - forKeys:[NSArray arrayWithObjects:@"access_token", @"expires", @"secret", @"session_key", @"sig", @"uid", nil]]; - NSDictionary* status = [NSDictionary - dictionaryWithObjects:[NSArray arrayWithObjects:@"connected", session, nil] - forKeys:[NSArray arrayWithObjects:@"status", @"session", nil]]; - - - PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsDictionary:status]; - NSString* callback = [result toSuccessCallbackString:self.loginCallbackId]; - // we need to wrap the callback in a setTimeout(func, 0) so it doesn't block the UI (handleOpenURL limitation) - [super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]]; + //get the UID of the currently logged in user + [facebook requestWithGraphPath:@"me" andDelegate:self]; } } @@ -56,8 +44,8 @@ - (void) init:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options NSString* callbackId = [arguments objectAtIndex:0]; NSString* appId = [arguments objectAtIndex:1]; - self.facebook = [[Facebook alloc] initWithAppId:appId andDelegate: self]; - + self.facebook = [[Facebook alloc] initWithAppId:appId]; + PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_OK]; [super writeJavascript:[result toSuccessCallbackString:callbackId]]; } @@ -94,7 +82,7 @@ - (void) login:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options // save the callbackId for handleOpenURL return (only works if the app is multi-tasked!) self.loginCallbackId = callbackId; - return [facebook authorize:marray]; + return [facebook authorize:marray delegate:self]; } @@ -131,36 +119,6 @@ - (void) dealloc [super dealloc]; } -/** - * Called when the user successfully logged in. - */ -- (void)fbDidLogin -{ - // [facebook dialog:@"feed" andDelegate:self]; - //[facebook requestWithGraphPath:@"me/friends" andDelegate:self]; - NSString* jsResult = [NSString stringWithFormat:@"FacebookGap.onLogin();"]; - [self.webView stringByEvaluatingJavaScriptFromString:jsResult]; - -} - -/** - * Called when the user dismissed the dialog without logging in. - */ -- (void)fbDialogNotLogin:(BOOL)cancelled -{ - // this NEVER seems to happen - NSString* jsResult = [NSString stringWithFormat:@"FacebookGap.onDidNotLogin(%d);",cancelled]; - [self.webView stringByEvaluatingJavaScriptFromString:jsResult]; -} - -/** - * Called when the user logged out. - */ -- (void)fbDidLogout -{ - NSString* jsResult = [NSString stringWithFormat:@"FacebookGap.onLogout();"]; - [self.webView stringByEvaluatingJavaScriptFromString:jsResult]; -} //////////////////////////////////////////////////////////////////// // FBRequestDelegate @@ -191,15 +149,24 @@ - (void)request:(FBRequest *)request didFailWithError:(NSError *)error /** * Called when a request returns and its response has been parsed into - * an object. + * an object. This is called only by the Graph API call to get the UID * * 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 -{ - NSString* jsResult = [NSString stringWithFormat:@"fbRequestResult(%@);", [result JSONRepresentation]]; - [self.webView stringByEvaluatingJavaScriptFromString:jsResult]; +{ + NSDictionary* session = [NSDictionary + dictionaryWithObjects:[NSArray arrayWithObjects:self.facebook.accessToken, [self.facebook.expirationDate description], APP_SECRET, [NSNumber numberWithBool:YES], @"...", [result valueForKey:@"id"], nil] + forKeys:[NSArray arrayWithObjects:@"access_token", @"expires", @"secret", @"session_key", @"sig", @"uid", nil]]; + NSDictionary* status = [NSDictionary + dictionaryWithObjects:[NSArray arrayWithObjects:@"connected", session, nil] + forKeys:[NSArray arrayWithObjects:@"status", @"session", nil]]; + + PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsDictionary:status]; + NSString* callback = [pluginResult toSuccessCallbackString:self.loginCallbackId]; + // we need to wrap the callback in a setTimeout(func, 0) so it doesn't block the UI (handleOpenURL limitation) + [super writeJavascript:[NSString stringWithFormat:@"setTimeout(function() { %@; }, 0);", callback]]; } ///** @@ -222,7 +189,7 @@ - (void)request:(FBRequest *)request didLoad:(id)result */ - (void)dialogDidComplete:(FBDialog *)dialog { - + // TODO } /** @@ -230,7 +197,7 @@ - (void)dialogDidComplete:(FBDialog *)dialog */ - (void)dialogCompleteWithUrl:(NSURL *)url { - + // TODO } /** @@ -238,7 +205,7 @@ - (void)dialogCompleteWithUrl:(NSURL *)url */ - (void)dialogDidNotCompleteWithUrl:(NSURL *)url { - + // TODO } /** @@ -246,7 +213,7 @@ - (void)dialogDidNotCompleteWithUrl:(NSURL *)url */ - (void)dialogDidNotComplete:(FBDialog *)dialog { - + // TODO } /** diff --git a/www/pg-plugin-fb-connect.js b/www/pg-plugin-fb-connect.js index 4d70371f5..b6ed673a3 100755 --- a/www/pg-plugin-fb-connect.js +++ b/www/pg-plugin-fb-connect.js @@ -1,6 +1,12 @@ PG = ( typeof PG == 'undefined' ? {} : PG ); PG.FB = { init: function(apiKey) { + // create the fb-root element if it doesn't exist + if (!document.getElementById('fb-root')) { + var elem = document.createElement('div'); + elem.id = 'fb-root'; + document.body.appendChild(elem); + } PhoneGap.exec(null, null, 'com.phonegap.facebook.Connect', 'init', [apiKey]); }, login: function(a, b) {