Skip to content
Browse files

Add support for setting the uid in the session for ios

  • Loading branch information...
1 parent 88915f3 commit 1b5c17e9d15593b1aea31df06f3e3778c2cbeb37 Dave Johnson committed Aug 22, 2011
View
71 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,31 +189,31 @@ - (void)request:(FBRequest *)request didLoad:(id)result
*/
- (void)dialogDidComplete:(FBDialog *)dialog
{
-
+ // TODO
}
/**
* Called when the dialog succeeds with a returning url.
*/
- (void)dialogCompleteWithUrl:(NSURL *)url
{
-
+ // TODO
}
/**
* Called when the dialog get canceled by the user.
*/
- (void)dialogDidNotCompleteWithUrl:(NSURL *)url
{
-
+ // TODO
}
/**
* Called when the dialog is cancelled and is about to be dismissed.
*/
- (void)dialogDidNotComplete:(FBDialog *)dialog
{
-
+ // TODO
}
/**
View
3 example/iOS/www/index.html
@@ -9,9 +9,6 @@
<button onclick="logout()">Logout</button>
<div id="data"></div>
-
- <!-- TODO: have this written dynamically in init if phonegap -->
- <div id="fb-root"></div>
<!-- phonegap -->
<script src="phonegap-1.0.0.js"></script>
View
77 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,31 +189,31 @@ - (void)request:(FBRequest *)request didLoad:(id)result
*/
- (void)dialogDidComplete:(FBDialog *)dialog
{
-
+ // TODO
}
/**
* Called when the dialog succeeds with a returning url.
*/
- (void)dialogCompleteWithUrl:(NSURL *)url
{
-
+ // TODO
}
/**
* Called when the dialog get canceled by the user.
*/
- (void)dialogDidNotCompleteWithUrl:(NSURL *)url
{
-
+ // TODO
}
/**
* Called when the dialog is cancelled and is about to be dismissed.
*/
- (void)dialogDidNotComplete:(FBDialog *)dialog
{
-
+ // TODO
}
/**
View
6 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) {

0 comments on commit 1b5c17e

Please sign in to comment.
Something went wrong with that request. Please try again.