Skip to content

Commit

Permalink
Merge pull request #155 from BranchMetrics/fix-nondeeplinkhandler
Browse files Browse the repository at this point in the history
Fix non Branch link handler
  • Loading branch information
aaustin authored Jun 25, 2016
2 parents d2b031c + 3c305ad commit 0a08888
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ SOFTWARE.
</intent-filter>
</config-file>
<source-file src="src/android/io/branch/BranchSDK.java" target-dir="src/io/branch" />
<framework src="io.branch.sdk.android:library:1.13.1" />
<framework src="io.branch.sdk.android:library:1+" />
</platform>

<!-- ios -->
Expand Down
26 changes: 20 additions & 6 deletions src/android/io/branch/BranchSDK.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.ArrayList;
import java.util.Iterator;
import android.net.Uri;

import io.branch.indexing.BranchUniversalObject;
import io.branch.referral.Branch;
Expand All @@ -33,6 +34,7 @@ static class BranchLinkProperties extends io.branch.referral.util.LinkProperties
private ArrayList<BranchUniversalObjectWrapper> branchObjectWrappers;
private Activity activity;
private Branch instance;
private String deepLinkUrl;

/**
* Class Constructor
Expand Down Expand Up @@ -221,9 +223,14 @@ private void initSession(CallbackContext callbackContext)
{
this.activity = this.cordova.getActivity();

Uri data = activity.getIntent().getData();
if (data != null && data.isHierarchical()) {
this.deepLinkUrl = data.toString();
}

this.instance = Branch.getAutoInstance(this.activity.getApplicationContext());

this.instance.initSession(new SessionListener(callbackContext), activity.getIntent().getData(), activity);
this.instance.initSession(new SessionListener(callbackContext), data, activity);
}

/**
Expand Down Expand Up @@ -710,14 +717,21 @@ public void onInitFinished(JSONObject referringParams, BranchError error) {
this._callbackContext.success(referringParams);
}

} else {
String errorMessage = error.getMessage();

out = String.format("NonBranchLinkHandler(\"%s\")", error.toString());
} else if (deepLinkUrl != null) {
JSONObject message = new JSONObject();
try {
message.put("error", "Not a Branch link!");
message.put("url", deepLinkUrl);
deepLinkUrl = null;
} catch (JSONException e) {
e.printStackTrace();
}

out = String.format("NonBranchLinkHandler(\"%s\")", message.toString());
webView.sendJavascript(out);

if (this._callbackContext != null) {
this._callbackContext.error(errorMessage);
this._callbackContext.error(message);
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/ios/BranchSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

@interface BranchSDK()

@property (strong, nonatomic) NSString *deepLinkUrl;

- (void)doShareLinkResponse:(int)callbackId sendResponse:(NSDictionary*)response;

@end
Expand Down Expand Up @@ -51,6 +53,7 @@ - (id)handleDeepLink:(CDVInvokedUrlCommand*)command
{
NSString *arg = [command.arguments objectAtIndex:0];
NSURL *url = [NSURL URLWithString:arg];
self.deepLinkUrl = [url absoluteString];

Branch *branch = [self getInstance];
return [NSNumber numberWithBool:[branch handleDeepLink:url]];
Expand All @@ -65,6 +68,10 @@ - (void)continueUserActivity:(CDVInvokedUrlCommand*)command
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:activityType];
[userActivity setUserInfo:userInfo];

if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
self.deepLinkUrl = [userActivity.webpageURL absoluteString];
}

Branch *branch = [self getInstance];

[branch continueUserActivity:userActivity];
Expand Down Expand Up @@ -125,6 +132,10 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
if (isFromBranchLink) {
[self.commandDelegate evalJs:[NSString stringWithFormat:@"DeepLinkHandler(%@)", resultString]];
}
else if (self.deepLinkUrl) {
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"BSDKPostUnhandledURL" object:self.deepLinkUrl]];
self.deepLinkUrl = nil;
}

if (command != nil) {
[self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
Expand Down Expand Up @@ -590,7 +601,7 @@ - (void)postUnhandledURL:(NSNotification *)notification {
}

if ([urlString isKindOfClass:[NSString class]]) {
NSDictionary *returnDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Unable to process URL", @"error", urlString, @"url", nil];
NSDictionary *returnDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Not a Branch link!", @"error", urlString, @"url", nil];
NSData* returnJSON = [NSJSONSerialization dataWithJSONObject:returnDict
options:NSJSONWritingPrettyPrinted
error:&error];
Expand Down

0 comments on commit 0a08888

Please sign in to comment.