Skip to content

Commit

Permalink
Add support for setting the uid in the session for ios
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Johnson committed Aug 22, 2011
1 parent 88915f3 commit 1b5c17e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 110 deletions.
Expand Up @@ -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];
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]];
}

///**
Expand All @@ -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
}

/**
Expand Down
3 changes: 0 additions & 3 deletions example/iOS/www/index.html
Expand Up @@ -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>
Expand Down
77 changes: 22 additions & 55 deletions native/ios/FacebookConnectPlugin.m
Expand Up @@ -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];
}
}

Expand All @@ -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]];
}
Expand Down Expand Up @@ -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];

}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]];
}

///**
Expand All @@ -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
}

/**
Expand Down
6 changes: 6 additions & 0 deletions 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) {
Expand Down

0 comments on commit 1b5c17e

Please sign in to comment.