Permalink
Browse files

Updated plugin to work with 1.0rc1. Patched facebook.js with PhoneGap…

… additions (from Dave).
  • Loading branch information...
1 parent c23165a commit f499f9395a8bbf099a14ab63d43aa3c84c0ed6a3 @shazron shazron committed Jul 8, 2011
Showing with 188 additions and 78 deletions.
  1. +6 −4 native/ios/FacebookConnectPlugin.h
  2. +67 −18 native/ios/FacebookConnectPlugin.m
  3. +40 −15 www/facebook-connect-0.0.1.js
  4. +36 −18 www/facebook.js
  5. +39 −23 www/index.html
View
10 native/ios/FacebookConnectPlugin.h 100644 → 100755
@@ -7,13 +7,15 @@
//
#import <Foundation/Foundation.h>
-#import "PhoneGapCommand.h"
+#ifdef PHONEGAP_FRAMEWORK
+ #import <PhoneGap/PGPlugin.h>
+#else
+ #import "PGPlugin.h"
+#endif
#import "FBConnect.h"
-@interface FacebookConnectPlugin : PhoneGapCommand<FBSessionDelegate,FBRequestDelegate,FBDialogDelegate> {
-
- Facebook *facebook;
+@interface FacebookConnectPlugin : PGPlugin<FBSessionDelegate,FBRequestDelegate,FBDialogDelegate> {
}
@property (nonatomic, retain) Facebook *facebook;
View
85 native/ios/FacebookConnectPlugin.m 100644 → 100755
@@ -7,46 +7,95 @@
//
#import "FacebookConnectPlugin.h"
-#import "JSON.h"
+#ifdef PHONEGAP_FRAMEWORK
+ #import <PhoneGap/JSON.h>
+ #import <PhoneGap/PluginResult.h>
+#else
+ #import "JSON.h"
+ #import "PluginResult.h"
+#endif
@implementation FacebookConnectPlugin
@synthesize facebook;
-- (void) initWithAppId:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+- (void) init:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
- NSString* appId = [arguments objectAtIndex:0];
- facebook = [[Facebook alloc] initWithAppId:appId];
-
-// TODO: pass in permissions
-// NSArray* permissions = [[NSArray arrayWithObjects:
-// @"email", @"read_stream", nil] retain];
-//
-//
-// [facebook authorize:permissions delegate:self];
+ if ([arguments count] < 2) {
+ return;
+ }
+
+ NSString* callbackId = [arguments objectAtIndex:0];
+ NSString* appId = [arguments objectAtIndex:1];
+ self.facebook = [[Facebook alloc] initWithAppId:appId];
+
+ PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_OK];
+ [super writeJavascript:[result toSuccessCallbackString:callbackId]];
}
--(void) authorize:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+- (void) getLoginStatus:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
- [facebook authorize:arguments delegate:self];
+ NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId
+
+ PluginResult* result = [PluginResult resultWithStatus:self.facebook? PGCommandStatus_OK : PGCommandStatus_ERROR];
+ [super writeJavascript:[result toSuccessCallbackString:callbackId]];
}
--(void) logout:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+- (void) login:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
+ if ([arguments count] < 2) {
+ return;
+ }
+
+ NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId
+
+ NSMutableArray* marray = [NSMutableArray arrayWithArray:arguments];
+ [marray removeObjectAtIndex:0]; // first item is the callbackId
+
+ [facebook authorize:marray delegate:self];
+
+ PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_NO_RESULT];
+ [super writeJavascript:[result toSuccessCallbackString:callbackId]];
+}
+
+- (void) logout:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+{
+ NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId
+
[facebook logout:self];
+
+ PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_NO_RESULT];
+ [super writeJavascript:[result toSuccessCallbackString:callbackId]];
}
--(void) handleOpenUrl:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+- (void) handleOpenUrl:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
- NSURL* url = [NSURL URLWithString:[arguments objectAtIndex:0]];
- [facebook handleOpenURL:url];
+ NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId
+
+ NSURL* url = [NSURL URLWithString:[arguments objectAtIndex:1]];
+ BOOL ok = [facebook handleOpenURL:url];
+
+
+ PluginResult* result = [PluginResult resultWithStatus:ok ? PGCommandStatus_OK : PGCommandStatus_ERROR];
+ [super writeJavascript:[result toSuccessCallbackString:callbackId]];
}
--(void) showFeedPublishDialog:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+- (void) showFeedPublishDialog:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
+ NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId
+
[facebook dialog:@"feed" andDelegate:self];
+
+ PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_NO_RESULT];
+ [super writeJavascript:[result toSuccessCallbackString:callbackId]];
+}
+
+- (void) dealloc
+{
+ self.facebook = nil;
+ [super dealloc];
}
/**
View
55 www/facebook-connect-0.0.1.js 100644 → 100755
@@ -1,27 +1,52 @@
PG = ( typeof PG == 'undefined' ? {} : PG );
-PG.FB: {
+PG.FB = {
init: function(apiKey) {
- PhoneGap.exec(null, null, 'com.facebook.phonegap.Connect', 'init', [apiKey]);
+ PhoneGap.exec(function(e) {
+ console.log("init: " + e);
+ }, null, 'com.facebook.phonegap.Connect', 'init', [apiKey]);
},
login: function(a, b) {
- b = b || { perms: '' };
- PhoneGap.exec(function(e) { // login
- FB.Auth.setSession(e.session, 'connected');
- if (a) a(e);
- }, null, 'com.facebook.phonegap.Connect', 'login', b.perms.split(',') );
+ try {
+ b = b || { perms: '' };
+ PhoneGap.exec(function(e) { // login
+ FB.Auth.setSession(e.session, 'connected');
+ if (a) a(e);
+ }, null, 'com.facebook.phonegap.Connect', 'login', b.perms.split(',') );
+ } catch (e) {
+ alert(e);
+ }
},
logout: function(cb) {
- PhoneGap.exec(function(e) {
- FB.Auth.setSession(null, 'notConnected');
- if (cb) cb(e);
- }, null, 'com.facebook.phonegap.Connect', 'logout', []);
+ try {
+ PhoneGap.exec(function(e) {
+ FB.Auth.setSession(null, 'notConnected');
+ if (cb) cb(e);
+ }, null, 'com.facebook.phonegap.Connect', 'logout', []);
+ } catch (e) {
+ alert(e);
+ }
},
getLoginStatus: function(cb) {
- PhoneGap.exec(function(e) {
- if (cb) cb(e);
- }, null, 'com.facebook.phonegap.Connect', 'getLoginStatus', []);
+ try {
+ PhoneGap.exec(function(e) {
+ if (cb) cb(e);
+ console.log("getLoginStatus: " + e);
+ }, null, 'com.facebook.phonegap.Connect', 'getLoginStatus', []);
+ } catch (e) {
+ alert(e);
+ }
+ },
+ handleOpenUrl: function(url) {
+ try {
+ console.log('PG.FB:' + url);
+ PhoneGap.exec(function(e) {
+ console.log("handleOpenUrl: " + e);
+ }, null, 'com.facebook.phonegap.Connect', 'handleOpenUrl', [url]);
+ } catch (e) {
+ alert(e);
+ }
},
isAppInstalled: function(cb) {
}
-};
+};
View
54 www/facebook.js 100644 → 100755
@@ -1288,27 +1288,45 @@ FB.provide('', {
return;
}
+// if (FB._phonegap) {
+// if (a.method == 'permissions.request') {
+// // TODO: check wtf b arg is all about...
+// PhoneGap.exec(function(e) { // login
+// FB.Auth.setSession(e.session, 'connected');
+// if (b) b(e);
+// }, null, 'com.facebook.phonegap.Connect', 'login', a.perms.split(',') );
+// return;
+// } else if (a.method == 'auth.logout') { // logout
+// PhoneGap.exec(function(e) {
+// FB.Auth.setSession(null, 'notConnected');
+// if (b) b(e);
+// }, null, 'com.facebook.phonegap.Connect', 'logout', []);
+// return;
+// } else if (a.method == 'auth.status') { // getLoginStatus
+// PhoneGap.exec(function(e) {
+// if (b) b(e);
+// }, null, 'com.facebook.phonegap.Connect', 'getLoginStatus', []);
+// return;
+// }
+// }
+
+ // If the phonegap arg is specified then call out to the phonegap plugin
+ // which uses the native app rather than using the iframe / popup web
if (FB._phonegap) {
- if (a.method == 'permissions.request') {
- // TODO: check wtf b arg is all about...
- PhoneGap.exec(function(e) { // login
- FB.Auth.setSession(e.session, 'connected');
- if (b) b(e);
- }, null, 'com.facebook.phonegap.Connect', 'login', a.perms.split(',') );
- return;
- } else if (a.method == 'auth.logout') { // logout
- PhoneGap.exec(function(e) {
- FB.Auth.setSession(null, 'notConnected');
- if (b) b(e);
- }, null, 'com.facebook.phonegap.Connect', 'logout', []);
- return;
- } else if (a.method == 'auth.status') { // getLoginStatus
- PhoneGap.exec(function(e) {
- if (b) b(e);
- }, null, 'com.facebook.phonegap.Connect', 'getLoginStatus', []);
- return;
+ switch (f.method) {
+ case 'permissions.request':
+ FB._phonegap.login(b, f);
+ break;
+ case 'auth.logout':
+ FB._phonegap.logout(b);
+ break;
+ case 'auth.status':
+ FB._phonegap.getLoginStatus(b);
+ break;
}
+ return;
}
+
if (f.method == 'permissions.request' && (f.display == 'iframe' || f.display == 'dialog')) {
var h = f.perms.split(',');
View
62 www/index.html 100644 → 100755
@@ -1,5 +1,14 @@
<html>
- <head></head>
+ <head>
+ <!-- phonegap -->
+ <script src="phonegap-0.9.6.js"></script>
+ <!-- phonegap facebook plugin -->
+ <script src="facebook-connect-0.0.1.js"></script>
+ <!-- weinre -->
+ <script src="http://debug.phonegap.com/target/target-script-min.js#foobook"></script>
+ <!-- facebook js sdk -->
+ <script src="facebook.js"></script>
+ </head>
<body>
<button onclick="init()">Init</button>
<button onclick="login()">Login</button>
@@ -11,14 +20,6 @@
<!-- TODO: have this written dynamically in init if phonegap -->
<div id="fb-root"></div>
- <!-- phonegap -->
- <script src="phonegap.0.9.5.js"></script>
- <!-- phonegap facebook plugin -->
- <script src="facebook-connect-0.0.1.js"></script>
- <!-- weinre -->
- <script src="http://debug.phonegap.com/target/target-script-min.js#foobook"></script>
- <!-- facebook js sdk -->
- <script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.Event.subscribe('auth.login', function(response) {
alert('auth.login event');
@@ -42,40 +43,55 @@
function getLoginStatus() {
FB.getLoginStatus(function(response) {
- if (response.session) {
- alert('logged in');
- } else {
- alert('not logged in');
- }
+ if (response.session) {
+ alert('logged in');
+ } else {
+ alert('not logged in');
+ }
});
}
function me() {
FB.api('/me/friends', function(response) {
- alert(JSON.stringify(response));
+ alert(JSON.stringify(response));
});
}
function logout() {
FB.logout(function(response) {
- alert('logged out');
+ alert('logged out');
});
}
function login() {
- FB.login(
- function(e) {alert('success');},
- function(e) {console.log('fail'); console.log(e)},
- ["email"]
- );
+ FB.login(
+ function(e) {alert(e);},
+ { perms: "email" }
+ );
}
function init() {
- FB.init({ appId: "188240441228254", phonegap: PG.FB });
+ try {
+ FB.init({ appId: "188240441228254", phonegap: PG.FB });
+ } catch (e) {
+ alert(e);
+ }
+ }
+
+ function handleOpenURL(url)
+ {
+ // handleOpenUrl MUST return immediately, so we cannot do any blocking methods in here -jm
+ setTimeout(
+ function()
+ {
+ PG.FB.handleOpenUrl(url);
+ },
+ 0);
+ return true;
}
document.addEventListener('deviceready', function() {
- navigator.app.addService("com.facebook.phonegap.Connect", "com.facebook.phonegap.ConnectPlugin");
+ navigator.app.addService("com.facebook.phonegap.Connect", "com.facebook.phonegap.ConnectPlugin");
}, false);
</script>
</body>

0 comments on commit f499f93

Please sign in to comment.