Skip to content

Commit

Permalink
fix for non-branch link handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jestoniyap committed Apr 13, 2016
1 parent 3a1f20e commit 82d9746
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/ios/AppDelegate+BranchSdk.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "AppDelegate.h"
#import <Branch/Branch.h>
#import <Cordova/CDV.h>

@interface AppDelegate (BranchSDK)

Expand All @@ -23,6 +24,8 @@ - (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;
}
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 82d9746

Please sign in to comment.