From 1cdc7b765575b617896d70865dbbc6cffba59ea4 Mon Sep 17 00:00:00 2001 From: Mehmet Karadayi Date: Sun, 6 Jan 2019 22:36:51 +0100 Subject: [PATCH] Enable app events to be sent from the pixel for both iOS and Android (#678) --- README.md | 2 +- plugin.xml | 9 ++++++++- src/android/ConnectPlugin.java | 23 +++++++++++++++++++++++ src/ios/FacebookConnectPlugin.m | 23 +++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 63cd75019..a10259ac6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ See npm package for versions - https://www.npmjs.com/package/cordova-plugin-face Make sure you've registered your Facebook app with Facebook and have an `APP_ID` [https://developers.facebook.com/apps](https://developers.facebook.com/apps). ```bash -$ cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="123456789" --variable APP_NAME="myApplication" +$ cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="123456789" --variable APP_NAME="myApplication" --variable ENABLE_HYBRID_APP_EVENTS="true" ``` 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. diff --git a/plugin.xml b/plugin.xml index 84f1e56d4..11a0f3f53 100644 --- a/plugin.xml +++ b/plugin.xml @@ -12,13 +12,14 @@ The Facebook plugin for Apache Cordova allows you to use the same JavaScript code in your Cordova application as you use in your web application. - + https://github.com/jeduan/cordova-plugin-facebook4 Apache 2.0 + @@ -52,11 +53,13 @@ $APP_ID $APP_NAME + $ENABLE_HYBRID_APP_EVENTS $APP_ID $APP_NAME + $ENABLE_HYBRID_APP_EVENTS @@ -104,6 +107,10 @@ $APP_NAME + + $ENABLE_HYBRID_APP_EVENTS + + diff --git a/src/android/ConnectPlugin.java b/src/android/ConnectPlugin.java index 3aa97ce22..1b90ab5b4 100644 --- a/src/android/ConnectPlugin.java +++ b/src/android/ConnectPlugin.java @@ -1,9 +1,12 @@ package org.apache.cordova.facebook; +import android.content.Context; import android.content.Intent; +import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.util.Log; +import android.webkit.WebView; import com.facebook.AccessToken; import com.facebook.CallbackManager; @@ -86,6 +89,9 @@ protected void pluginInitialize() { // create AppEventsLogger logger = AppEventsLogger.newLogger(cordova.getActivity().getApplicationContext()); + // augment web view to enable hybrid app events + enableHybridAppEvents(); + // Set up the activity result callback to this class cordova.setActivityResultCallback(this); @@ -701,6 +707,23 @@ private void executeLogin(JSONArray args, CallbackContext callbackContext) throw } } + private void enableHybridAppEvents() { + try { + Context appContext = cordova.getActivity().getApplicationContext(); + Resources res = appContext.getResources(); + int enableHybridAppEventsId = res.getIdentifier("enable_hybrid_app_events", "bool", appContext.getPackageName()); + boolean enableHybridAppEvents = enableHybridAppEventsId != 0 && res.getBoolean(enableHybridAppEventsId); + if (enableHybridAppEvents) { + AppEventsLogger.augmentWebView((WebView) this.webView.getView(), appContext); + Log.d(TAG, "Hybrid app events are enabled"); + } else { + Log.d(TAG, "Hybrid app events are not enabled"); + } + } catch (Exception e) { + Log.d(TAG, "Hybrid app events cannot be enabled"); + } + } + private ShareLinkContent buildContent(Map paramBundle) { ShareLinkContent.Builder builder = new ShareLinkContent.Builder(); if (paramBundle.containsKey("href")) diff --git a/src/ios/FacebookConnectPlugin.m b/src/ios/FacebookConnectPlugin.m index fb728c341..c58b57e1c 100644 --- a/src/ios/FacebookConnectPlugin.m +++ b/src/ios/FacebookConnectPlugin.m @@ -23,6 +23,7 @@ - (NSDictionary *)responseObject; - (NSDictionary*)parseURLParams:(NSString *)query; - (BOOL)isPublishPermission:(NSString*)permission; - (BOOL)areAllPermissionsReadPermissions:(NSArray*)permissions; +- (void)enableHybridAppEvents; @end @implementation FacebookConnectPlugin @@ -52,6 +53,7 @@ - (void) applicationDidFinishLaunching:(NSNotification *) notification { - (void) applicationDidBecomeActive:(NSNotification *) notification { [FBSDKAppEvents activateApp]; + [self enableHybridAppEvents]; } #pragma mark - Cordova commands @@ -649,6 +651,27 @@ - (BOOL)areAllPermissionsReadPermissions:(NSArray*)permissions { return YES; } +/* + * Enable the hybrid app events for the webview. + * This feature only works with WKWebView so until + * Cordova iOS 5 is relased + * (https://cordova.apache.org/news/2018/08/01/future-cordova-ios-webview.html), + * an additional plugin (e.g cordova-plugin-wkwebview-engine) is needed. + */ +- (void)enableHybridAppEvents { + if ([self.webView isMemberOfClass:[WKWebView class]]){ + NSString *is_enabled = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"EnableHybridAppEvents"]; + if([is_enabled isEqualToString:@"true"]){ + [FBSDKAppEvents augmentHybridWKWebView:(WKWebView*)self.webView]; + NSLog(@"Hybrid app events are enabled!"); + } else { + NSLog(@"Hybrid app events are not enabled!"); + } + } else { + NSLog(@"Hybrid app events cannot be enabled, this feature requires WKWebView"); + } +} + # pragma mark - FBSDKSharingDelegate - (void)sharer:(id)sharer didCompleteWithResults:(NSDictionary *)results {