Skip to content

Commit

Permalink
Merge pull request Wizcorp#3 from shazron/plugin-fix-3
Browse files Browse the repository at this point in the history
Final plugin fix for ios
  • Loading branch information
Dave Johnson committed Jul 11, 2011
2 parents 62de0a1 + 9a1e1a7 commit 8e36f79
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 85 deletions.
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -13,3 +13,22 @@ simply run:
./run-tests

The tests can be found in `./test/www/facebook-connect-tests.js`.

iOS Testing
-----------

Make sure you add the scheme to your [PROJECTNAME]-Info.plist, substitute [APP_ID] and [SCHEME_ID] below to the appropriate values. This is to handle the re-direct from Mobile Safari, after permission authorization.

<pre>
&lt;key&gt;CFBundleURLTypes&lt;/key&gt;
&lt;array&gt;
&lt;dict&gt;
&lt;key&gt;CFBundleURLName&lt;/key&gt;
&lt;string&gt;[SCHEME_ID]&lt;/string&gt;
&lt;key&gt;CFBundleURLSchemes&lt;/key&gt;
&lt;array&gt;
&lt;string&gt;fb[APP_ID]&lt;/string&gt;
&lt;/array&gt;
&lt;/dict&gt;
&lt;/array&gt;
</pre>
10 changes: 6 additions & 4 deletions native/ios/FacebookConnectPlugin.h 100644 → 100755
Expand Up @@ -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;
Expand Down
111 changes: 92 additions & 19 deletions native/ios/FacebookConnectPlugin.m 100644 → 100755
Expand Up @@ -7,46 +7,119 @@
//

#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

#define APP_SECRET @"b082c4620cdac27e0371f2c674026662"

@implementation FacebookConnectPlugin

@synthesize facebook;

/* This overrides PGPlugin's method, which receives a notification when handleOpenURL is called on the main app delegate */
- (void) handleOpenURL:(NSNotification*)notification
{
NSURL* url = [notification object];
if (![url isKindOfClass:[NSURL class]]) {
return;
}

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]];

[super writeJavascript:[NSString stringWithFormat:@"FB.Auth.setSession(%@);", [session JSONRepresentation]]];
}
}

- (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
{
[facebook logout:self];
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;

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];
}

[super writeJavascript:jsString];
}

-(void) handleOpenUrl:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
- (void) logout:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSURL* url = [NSURL URLWithString:[arguments objectAtIndex:0]];
[facebook handleOpenURL:url];
if (!self.facebook) {
return;
}

NSString* callbackId = [arguments objectAtIndex:0];// first item is the callbackId

[facebook logout:self];

PluginResult* result = [PluginResult resultWithStatus:PGCommandStatus_OK];
[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];
}

/**
Expand Down
45 changes: 30 additions & 15 deletions www/facebook-connect-0.0.1.js 100644 → 100755
@@ -1,27 +1,42 @@
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'); // 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) {
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);
}
},
isAppInstalled: function(cb) {

}
};
};

0 comments on commit 8e36f79

Please sign in to comment.