Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Set the session in Javascript through handleOpenUrl (UI Login process)

  • Loading branch information...
commit ba3e79d113121aace936198dbfa70860680259ce 1 parent f499f93
Shazron Abdullah shazron authored
85 native/ios/FacebookConnectPlugin.m
View
@@ -15,6 +15,7 @@
#import "PluginResult.h"
#endif
+#define APP_SECRET @"b082c4620cdac27e0371f2c674026662"
@implementation FacebookConnectPlugin
@@ -45,41 +46,101 @@ - (void) getLoginStatus:(NSMutableArray*)arguments withDict:(NSMutableDictionary
- (void) login:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
- if ([arguments count] < 2) {
+ if ([arguments count] < 2 || !self.facebook) {
return;
}
-
+
NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId
+ BOOL validSession = [self.facebook isSessionValid];
+
+ PluginResult* result = nil;
+ NSString* jsString = nil;
- NSMutableArray* marray = [NSMutableArray arrayWithArray:arguments];
- [marray removeObjectAtIndex:0]; // first item is the callbackId
+ if (validSession)
+ {
+ result = [PluginResult resultWithStatus:PGCommandStatus_OK];
+ jsString = [result toSuccessCallbackString:callbackId];
+
+ } else {
+ NSMutableArray* marray = [NSMutableArray arrayWithArray:arguments];
+ [marray removeObjectAtIndex:0]; // first item is the callbackId
+
+ [facebook authorize:marray delegate:self];
+
+ result = [PluginResult resultWithStatus:PGCommandStatus_ERROR messageAsString:@"Must call FB.init before FB.login"];
+ jsString = [result toErrorCallbackString:callbackId];
+ }
- [facebook authorize:marray delegate:self];
-
- PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_NO_RESULT];
- [super writeJavascript:[result toSuccessCallbackString:callbackId]];
+ [super writeJavascript:jsString];
}
- (void) logout:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
+ if (!self.facebook) {
+ return;
+ }
+
NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId
[facebook logout:self];
- PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_NO_RESULT];
+ PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_OK];
[super writeJavascript:[result toSuccessCallbackString:callbackId]];
}
- (void) handleOpenUrl:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
+ if (!self.facebook) {
+ return;
+ }
+
NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId
NSURL* url = [NSURL URLWithString:[arguments objectAtIndex:1]];
BOOL ok = [facebook handleOpenURL:url];
+
+ PluginResult* result = nil;
+ NSString* jsString = nil;
+
+ 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]];
+
+ NSString* statusValue = [self.facebook isSessionValid]? @"connected" : @"unknown";
+ NSDictionary* response = [NSDictionary
+ dictionaryWithObjects:[NSArray arrayWithObjects:statusValue, session, nil]
+ forKeys:[NSArray arrayWithObjects:@"status", @"session", nil]];
+
+
+// NSDictionary* response = [NSString stringWithFormat:
+// @"{ \
+// 'status' : '%@', \
+// 'session' : { \
+// 'access_token' : '%@', \
+// 'expires' : '%@', \
+// 'secret' : '%@', \
+// 'session_key' : true, \
+// 'sig' : '...', \
+// 'uid' : '...' \
+// } \
+// }",
+// [self.facebook isSessionValid]? @"connected" : @"unknown",
+// self.facebook.accessToken,
+// self.facebook.expirationDate,
+// APP_SECRET
+// ];
+
+ result = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsDictionary:response];
+ jsString = [result toSuccessCallbackString:callbackId];
+
+ } else {
+ result = [PluginResult resultWithStatus:PGCommandStatus_ERROR];
+ jsString = [result toErrorCallbackString:callbackId];
+ }
-
- PluginResult* result = [PluginResult resultWithStatus:ok ? PGCommandStatus_OK : PGCommandStatus_ERROR];
- [super writeJavascript:[result toSuccessCallbackString:callbackId]];
+ [super writeJavascript:jsString];
}
- (void) showFeedPublishDialog:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
5 www/facebook-connect-0.0.1.js
View
@@ -9,7 +9,7 @@ PG.FB = {
try {
b = b || { perms: '' };
PhoneGap.exec(function(e) { // login
- FB.Auth.setSession(e.session, 'connected');
+ //FB.Auth.setSession(e.session, 'connected'); // never gets called because the plugin spawns Mobile Safari/Facebook app
if (a) a(e);
}, null, 'com.facebook.phonegap.Connect', 'login', b.perms.split(',') );
} catch (e) {
@@ -38,9 +38,8 @@ PG.FB = {
},
handleOpenUrl: function(url) {
try {
- console.log('PG.FB:' + url);
PhoneGap.exec(function(e) {
- console.log("handleOpenUrl: " + e);
+ FB.Auth.setSession(e.session, 'connected');
}, null, 'com.facebook.phonegap.Connect', 'handleOpenUrl', [url]);
} catch (e) {
alert(e);
4 www/index.html
View
@@ -26,15 +26,17 @@
});
FB.Event.subscribe('auth.logout', function(response) {
- alert('auth.login event');
+ alert('auth.logout event');
});
FB.Event.subscribe('auth.sessionChange', function(response) {
alert('auth.sessionChange event');
+ console.log("auth.sessionChange: " + JSON.stringify(response));
});
FB.Event.subscribe('auth.statusChange', function(response) {
alert('auth.statusChange event');
+ console.log("auth.statusChange: " + JSON.stringify(response));
});
function getSession() {
Please sign in to comment.
Something went wrong with that request. Please try again.