Skip to content

Commit

Permalink
Merge pull request #98 from BranchMetrics/fix/issue-89-non-branch-lin…
Browse files Browse the repository at this point in the history
…ks-handler

Fix/issue 89 non branch links handler
  • Loading branch information
aaustin committed Apr 16, 2016
2 parents 6702d55 + d2bf25a commit 5a5e3ab
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ If you use a custom domain or subdomain for your Branch links, you should also a

---------------

## Non-Branch Links
There are instances where non-branch links are detected by the plugin but not processed.
You can retrieve the url by implementing the method `NonBranchLinkHandler()` which will act as our callback to return the non-branch url.

To implement:

```js
function NonBranchLinkHandler(data) {
if (data) {
alert('Non-Branch Link Detected: ' + JSON.stringify(data));
}
}
```
---------------

## Plugin Methods

**Most methods are promisified**, therefore you can easily get its success and error callback by chaining `.then()` method.
Expand Down
9 changes: 8 additions & 1 deletion src/ios/AppDelegate+BranchSdk.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceAppl
if (![[Branch getInstance] handleDeepLink:url]) {
// do other deep link routing for the Facebook SDK, Pinterest SDK, etc
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
// send unhandled URL to notification
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"BSDKPostUnhandledURL" object:url]];
}
return YES;
}

// Respond to Universal Links
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
BOOL handledByBranch = [[Branch getInstance] continueUserActivity:userActivity];
if (![[Branch getInstance] continueUserActivity:userActivity]) {
// send unhandled URL to notification
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"BSDKPostUnhandledURL" object:[userActivity.webpageURL absoluteString]]];
}
}

return handledByBranch;
}
Expand Down
49 changes: 33 additions & 16 deletions src/ios/BranchSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ - (void)pluginInitialize
{
NSLog(@"start pluginInitialize");
self.branchUniversalObjArray = [[NSMutableArray alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postUnhandledURL:) name:@"BSDKPostUnhandledURL" object:nil];
}

#pragma mark - Private APIs
Expand Down Expand Up @@ -82,10 +84,10 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
BOOL isFromUniversalLink = NO;
NSString *resultString = nil;
CDVPluginResult *pluginResult = nil;

// NOTE: For Universal Links. Using clicked_branch_link key as condition at the moment to identify if block is run due to Universal Links.
isFromUniversalLink = [[params objectForKey:@"+clicked_branch_link"] boolValue];

if (!error) {
if (params != nil && [params count] > 0) {
NSLog(@"Success");
Expand All @@ -101,7 +103,7 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
NSData* errorJSON = [NSJSONSerialization dataWithJSONObject:errorDict
options:NSJSONWritingPrettyPrinted
error:&err];

resultString = [[NSString alloc] initWithData:errorJSON encoding:NSUTF8StringEncoding];
} else {
NSLog(@"Success");
Expand All @@ -115,18 +117,18 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
}
else {
NSLog(@"Init Error: %@", [error localizedDescription]);

// We create a JSON string result, because we're getting an error if we directly return a string result.
NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:[error localizedDescription], @"error", nil];
NSData* errorJSON = [NSJSONSerialization dataWithJSONObject:errorDict
options:NSJSONWritingPrettyPrinted
error:&error];

resultString = [[NSString alloc] initWithData:errorJSON encoding:NSUTF8StringEncoding];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:resultString];
}
NSLog(@"returning data to js interface..");

if (isFromUniversalLink) {
NSLog(@"Sending to DeepLinkHandler: %@", resultString);
[self.commandDelegate evalJs:[NSString stringWithFormat:@"DeepLinkHandler(%@)", resultString]];
Expand All @@ -148,9 +150,9 @@ - (void)setDebug:(CDVInvokedUrlCommand*)command
if (enableDebug) {
[[Branch getInstance] setDebug];
}

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:enableDebug];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

Expand Down Expand Up @@ -248,7 +250,7 @@ - (void)userCompletedAction:(CDVInvokedUrlCommand*)command
else {
[branch userCompletedAction:name];
}

// TODO: need to resolve according to result of userCompletedAction, but no callback version of the method is exposed.
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"Success"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand Down Expand Up @@ -397,7 +399,7 @@ - (void)createBranchUniversalObject:(CDVInvokedUrlCommand*)command
NSNumber *branchUniversalObjectId = [[NSNumber alloc] initWithInteger:([self.branchUniversalObjArray count] - 1)];
NSString *message = @"createBranchUniversalObject Success";
NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:message, @"message", branchUniversalObjectId, @"branchUniversalObjectId", nil];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params];
NSLog(@"returning data to js interface..");
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand All @@ -407,9 +409,9 @@ - (void)registerView:(CDVInvokedUrlCommand*)command
{
NSLog(@"start registerView");
int branchUniversalObjectId = [[command.arguments objectAtIndex:0] intValue];

BranchUniversalObject *branchUniversalObj = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId];

[branchUniversalObj registerViewWithCallback:^(NSDictionary *params, NSError *error) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params];
NSLog(@"returning data to js interface..");
Expand All @@ -420,11 +422,11 @@ - (void)registerView:(CDVInvokedUrlCommand*)command
- (void)generateShortUrl:(CDVInvokedUrlCommand*)command
{
NSLog(@"start generateShortUrl");

int branchUniversalObjectId = [[command.arguments objectAtIndex:0] intValue];
NSDictionary *arg1 = [command.arguments objectAtIndex:1];
NSDictionary *arg2 = [command.arguments objectAtIndex:2];

BranchUniversalObject *branchUniversalObj = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId];

BranchLinkProperties *props = [[BranchLinkProperties alloc] init];
Expand Down Expand Up @@ -473,11 +475,11 @@ - (void)showShareSheet:(CDVInvokedUrlCommand*)command
if ([command.arguments count] >= 4) {
shareText = [command.arguments objectAtIndex:3];
}

int branchUniversalObjectId = [[command.arguments objectAtIndex:0] intValue];
NSDictionary *arg1 = [command.arguments objectAtIndex:1];
NSDictionary *arg2 = [command.arguments objectAtIndex:2];

BranchUniversalObject *branchUniversalObj = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId];

BranchLinkProperties *linkProperties = [[BranchLinkProperties alloc] init];
Expand Down Expand Up @@ -532,6 +534,21 @@ - (void)listOnSpotlight:(CDVInvokedUrlCommand*)command {
}];
}

#pragma mark - Private Methods
- (void)postUnhandledURL:(NSNotification *)notification {
// We create a JSON string result, because we're unable to handle the url. We will include the url in the return string.
NSError *error;
NSString *urlString = [notification.object absoluteString];
NSDictionary *returnDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Unable to process URL", @"error", urlString, @"url", nil];
NSData* returnJSON = [NSJSONSerialization dataWithJSONObject:returnDict
options:NSJSONWritingPrettyPrinted
error:&error];

NSString *resultString = [[NSString alloc] initWithData:returnJSON encoding:NSUTF8StringEncoding];
NSLog(@"Sending to DeepLinkHandler: %@", resultString);
[self.commandDelegate evalJs:[NSString stringWithFormat:@"NonBranchLinkHandler(%@)", resultString]];
}

#pragma mark - URL Methods (not fully implemented YET!)

- (NSString *)getShortURL:(CDVInvokedUrlCommand*)command
Expand Down
7 changes: 7 additions & 0 deletions testbed/www/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ function DeepLinkHandler(data)
}
}

function NonBranchLinkHandler(data)
{
if (data) {
alert('Non-branch link found: ' + JSON.stringify(data));
}
}

function SetDebug(isEnabled)
{
console.log('Trigger SetDebug()');
Expand Down

0 comments on commit 5a5e3ab

Please sign in to comment.