Skip to content

Commit

Permalink
Enable app events to be sent from the pixel for both iOS and Android (j…
Browse files Browse the repository at this point in the history
  • Loading branch information
msencer committed Jan 6, 2019
1 parent cf4728a commit 1cdc7b7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
</description>

<repo>https://github.com/jeduan/cordova-plugin-facebook4</repo>

<license>Apache 2.0</license>

<preference name="APP_ID" />
<preference name="APP_NAME" />
<preference name="ENABLE_HYBRID_APP_EVENTS" default="false" />
<preference name="FACEBOOK_ANDROID_SDK_VERSION" default="4.38.1"/>

<engines>
Expand Down Expand Up @@ -52,11 +53,13 @@
<config-file target="res/values/facebookconnect.xml" parent="/*">
<string name="fb_app_id">$APP_ID</string>
<string name="fb_app_name">$APP_NAME</string>
<bool name="enable_hybrid_app_events">$ENABLE_HYBRID_APP_EVENTS</bool>
</config-file>
<!-- Used for cordova-android 7 -->
<config-file target="app/src/main/res/values/facebookconnect.xml" parent="/*">
<string name="fb_app_id">$APP_ID</string>
<string name="fb_app_name">$APP_NAME</string>
<bool name="enable_hybrid_app_events">$ENABLE_HYBRID_APP_EVENTS</bool>
</config-file>

<config-file target="AndroidManifest.xml" parent="application">
Expand Down Expand Up @@ -104,6 +107,10 @@
<string>$APP_NAME</string>
</config-file>

<config-file target="*-Info.plist" parent="EnableHybridAppEvents">
<string>$ENABLE_HYBRID_APP_EVENTS</string>
</config-file>

<config-file target="*-Info.plist" parent="CFBundleURLTypes">
<array>
<dict>
Expand Down
23 changes: 23 additions & 0 deletions src/android/ConnectPlugin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<String, String> paramBundle) {
ShareLinkContent.Builder builder = new ShareLinkContent.Builder();
if (paramBundle.containsKey("href"))
Expand Down
23 changes: 23 additions & 0 deletions src/ios/FacebookConnectPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ - (NSDictionary *)responseObject;
- (NSDictionary*)parseURLParams:(NSString *)query;
- (BOOL)isPublishPermission:(NSString*)permission;
- (BOOL)areAllPermissionsReadPermissions:(NSArray*)permissions;
- (void)enableHybridAppEvents;
@end

@implementation FacebookConnectPlugin
Expand Down Expand Up @@ -52,6 +53,7 @@ - (void) applicationDidFinishLaunching:(NSNotification *) notification {

- (void) applicationDidBecomeActive:(NSNotification *) notification {
[FBSDKAppEvents activateApp];
[self enableHybridAppEvents];
}

#pragma mark - Cordova commands
Expand Down Expand Up @@ -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<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {
Expand Down

0 comments on commit 1cdc7b7

Please sign in to comment.