Skip to content

Commit

Permalink
Fix openUrl handling bugs and add support for cordova-ios-5 (jeduan#751)
Browse files Browse the repository at this point in the history
* Update FacebookConnectPlugin.m

Fix jeduan#750

* Update README.md

* Upgraded facebook sdk to 5.0.2

* Fix calls to deprecated facebook sdk functions on android which caused runtime crash when called.

* Removing Bolts framework ios dependency which is not used in ios sdk v5 facebook/facebook-ios-sdk@0bdd850

* Modernized FBSDKLoginKit usage according to
facebook/facebook-ios-sdk@6c4ac12
facebook/facebook-ios-sdk@5eae2d7:

1. Replaced FBSDKLoginManagerRequestTokenHandler by FBSDKLoginManagerLoginResultBlock
2. Replaced logInWithReadPermissions and logInWithPublishPermissions by logInWithPermissions

Modernized FBSDKShareKit usage according to facebook/facebook-ios-sdk@9b682bb:

1. Updated FBSDKShareOpenGraphAction init to actionWithType
2. Replaced FBSDKGraphRequestHandler by FBSDKGraphRequestBlock

* removed irrelevant files
  • Loading branch information
guylando authored and peterpeterparker committed Jun 29, 2019
1 parent afa6fa0 commit 8633acd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ $ cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="12345678

If you need to change your `APP_ID` after installation, it's recommended that you remove and then re-add the plugin as above. Note that changes to the `APP_ID` value in your `config.xml` file will *not* be propagated to the individual platform builds.

IMPORTANT: This plugin works as is with cordova-ios 5 but if you use earlier version of cordova-ios then you need to add the code in the following comment to your CordovaLib/Classes/Public/CDVAppDelegate.m file which was added to your project as part of the cordova-ios ios platform template: https://github.com/apache/cordova-ios/issues/476#issuecomment-460907247

## Usage

This is a fork of the [official plugin for Facebook](https://github.com/Wizcorp/phonegap-facebook-plugin/) in Apache Cordova that implements the latest Facebook SDK. Unless noted, this is a drop-in replacement. You don't have to replace your client code.
Expand Down
5 changes: 1 addition & 4 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<preference name="APP_ID" />
<preference name="APP_NAME" />
<preference name="FACEBOOK_HYBRID_APP_EVENTS" default="false" />
<preference name="FACEBOOK_ANDROID_SDK_VERSION" default="4.40.0"/>
<preference name="FACEBOOK_ANDROID_SDK_VERSION" default="5.0.2"/>

<engines>
<!-- Requires > 3.5.0 because of the custom Framework tag for iOS [CB-6698] -->
Expand Down Expand Up @@ -160,9 +160,6 @@
</array>
</config-file>

<!-- Bolts framework -->
<framework src="src/ios/Bolts.framework" custom="true" />

<!-- Facebook framework -->
<framework src="src/ios/FBSDKCoreKit.framework" custom="true" />
<framework src="src/ios/FBSDKLoginKit.framework" custom="true" />
Expand Down
6 changes: 3 additions & 3 deletions src/android/ConnectPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,13 @@ public void onError(FacebookException e) {
public void onResume(boolean multitasking) {
super.onResume(multitasking);
// Developers can observe how frequently users activate their app by logging an app activation event.
AppEventsLogger.activateApp(cordova.getActivity());
AppEventsLogger.activateApp(cordova.getActivity().getApplication());
}

@Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
AppEventsLogger.deactivateApp(cordova.getActivity());
AppEventsLogger.deactivateApp(cordova.getActivity().getApplication());
}

@Override
Expand Down Expand Up @@ -305,7 +305,7 @@ public boolean execute(String action, JSONArray args, final CallbackContext call
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
AppEventsLogger.activateApp(cordova.getActivity());
AppEventsLogger.activateApp(cordova.getActivity().getApplication());
}
});

Expand Down
30 changes: 12 additions & 18 deletions src/ios/FacebookConnectPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ - (void)login:(CDVInvokedUrlCommand *)command {
// without refreshing there will be a cache problem. This simple call should fix the problems
[FBSDKAccessToken refreshCurrentAccessToken:nil];

FBSDKLoginManagerRequestTokenHandler loginHandler = ^void(FBSDKLoginManagerLoginResult *result, NSError *error) {
FBSDKLoginManagerLoginResultBlock loginHandler = ^void(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// If the SDK has a message for the user, surface it.
NSString *errorMessage = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?: @"There was a problem logging you in.";
Expand Down Expand Up @@ -188,7 +188,7 @@ - (void)login:(CDVInvokedUrlCommand *)command {
if (self.loginManager == nil) {
self.loginManager = [[FBSDKLoginManager alloc] init];
}
[self.loginManager logInWithReadPermissions:permissions fromViewController:[self topMostController] handler:loginHandler];
[self.loginManager logInWithPermissions:permissions fromViewController:[self topMostController] handler:loginHandler];
return;
}

Expand Down Expand Up @@ -326,17 +326,15 @@ - (void) showDialog:(CDVInvokedUrlCommand*)command
NSLog(@"There was an error parsing your 'object' JSON string");
} else {
FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:json];
FBSDKShareOpenGraphAction *action = [[FBSDKShareOpenGraphAction alloc] init];
action.actionType = params[@"action"];
if(!json[@"og:type"]) {
NSLog(@"No 'og:type' encountered in the object JSON. Please provide an Open Graph object type.");
return;
}
NSString *objectType = json[@"og:type"];
objectType = [objectType stringByReplacingOccurrencesOfString:@"."
withString:@":"];
FBSDKShareOpenGraphAction *action = [FBSDKShareOpenGraphAction actionWithType:params[@"action"] object:object key:objectType];

[action setObject:object forKey:objectType];
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init];
content.action = action;
content.previewPropertyName = objectType;
Expand Down Expand Up @@ -423,7 +421,7 @@ - (void) graphApi:(CDVInvokedUrlCommand *)command
permissions = [requestPermissions copy];

// Defines block that handles the Graph API response
FBSDKGraphRequestHandler graphHandler = ^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
FBSDKGraphRequestBlock graphHandler = ^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
CDVPluginResult* pluginResult;
if (error) {
NSString *message = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?: @"There was an error making the graph call.";
Expand All @@ -439,7 +437,7 @@ - (void) graphApi:(CDVInvokedUrlCommand *)command
};

NSLog(@"Graph Path = %@", graphPath);
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:graphPath parameters:nil];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:graphPath];

// If we have permissions to request
if ([permissions count] == 0){
Expand Down Expand Up @@ -510,7 +508,7 @@ - (void) activateApp:(CDVInvokedUrlCommand *)command

#pragma mark - Utility methods

- (void) loginWithPermissions:(NSArray *)permissions withHandler:(FBSDKLoginManagerRequestTokenHandler) handler {
- (void) loginWithPermissions:(NSArray *)permissions withHandler:(FBSDKLoginManagerLoginResultBlock) handler {
BOOL publishPermissionFound = NO;
BOOL readPermissionFound = NO;
if (self.loginManager == nil) {
Expand All @@ -537,13 +535,8 @@ - (void) loginWithPermissions:(NSArray *)permissions withHandler:(FBSDKLoginMana
};
NSError *error = [NSError errorWithDomain:@"facebook" code:-1 userInfo:userInfo];
handler(nil, error);

} else if (publishPermissionFound) {
// Only publish permissions
[self.loginManager logInWithPublishPermissions:permissions fromViewController:[self topMostController] handler:handler];
} else {
// Only read permissions
[self.loginManager logInWithReadPermissions:permissions fromViewController:[self topMostController] handler:handler];
[self.loginManager logInWithPermissions:permissions fromViewController:[self topMostController] handler:handler];
}
}

Expand Down Expand Up @@ -787,10 +780,11 @@ void FBMethodSwizzle(Class c, SEL originalSelector) {
+ (void)load
{
FBMethodSwizzle([self class], @selector(application:openURL:sourceApplication:annotation:));
FBMethodSwizzle([self class], @selector(application:openURL:options:));
}

// This method is a duplicate of the other openURL method below, except using the newer iOS (9) API.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
- (BOOL)swizzled_application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
if (!url) {
return NO;
}
Expand All @@ -800,10 +794,10 @@ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(N
// NOTE: Cordova will run a JavaScript method here named handleOpenURL. This functionality is deprecated
// but will cause you to see JavaScript errors if you do not have window.handleOpenURL defined:
// https://github.com/Wizcorp/phonegap-facebook-plugin/issues/703#issuecomment-63748816
NSLog(@"FB handle url: %@", url);
NSLog(@"FB handle url using application:openURL:options: %@", url);

// Call existing method
return [self swizzled_application:application openURL:url sourceApplication:[options valueForKey:@"UIApplicationOpenURLOptionsSourceApplicationKey"] annotation:0x0];
return [self swizzled_application:application openURL:url options:options];
}

- (BOOL)noop_application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
Expand All @@ -822,7 +816,7 @@ - (BOOL)swizzled_application:(UIApplication *)application openURL:(NSURL *)url s
// NOTE: Cordova will run a JavaScript method here named handleOpenURL. This functionality is deprecated
// but will cause you to see JavaScript errors if you do not have window.handleOpenURL defined:
// https://github.com/Wizcorp/phonegap-facebook-plugin/issues/703#issuecomment-63748816
NSLog(@"FB handle url: %@", url);
NSLog(@"FB handle url using application:openURL:sourceApplication:annotation: %@", url);

// Call existing method
return [self swizzled_application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
Expand Down

0 comments on commit 8633acd

Please sign in to comment.