diff --git a/README.md b/README.md index b03b6baa..eab217b0 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ If you use a custom domain or subdomain for your Branch links, you should also a ## Plugin Methods -**Some methods are promisified**, therefore you can easily get its success and error callback by chaining `.then()` method. +**Most methods are promisified**, therefore you can easily get its success and error callback by chaining `.then()` method. *Example* ```js @@ -168,13 +168,15 @@ The `initSession()` method automatically also sets an internal deep link hander ```js onDeviceReady: function() { - Branch.initSession(); -}, -onResume: function() { - Branch.initSession(); + Branch.initSession().then(function (res) { + console.log(res); + alert('Response: ' + JSON.stringify(res)); + }).catch(function (err) { + console.error(err); + alert('Error: ' + JSON.stringify(err)); + });;; }, initialize: function() { - document.addEventListener('resume', onResume, false); document.addEventListener('deviceready', onDeviceReady, false); }, ``` @@ -183,7 +185,11 @@ Then add the method `DeepLinkHandler()` which will act as our callback when the ```js function DeepLinkHandler(data) { - alert('Data from initSession: ' + data.data); + if (data) { + alert('Data from deep link: ' + JSON.stringify(data)); + } else { + alert('No data found'); + } } ``` @@ -243,7 +249,11 @@ Logs out the current session, replaces session IDs and identity IDs. ##### Usage ```js -Branch.logout(); +Branch.logout().then(function (res) { + console.log(res); +}).catch(function (err) { + console.error(err); +}); ``` ### userCompletedAction(action[, metaData]) @@ -258,9 +268,17 @@ Registers custom events. ##### Usage ```js -Branch.userCompletedAction('complete_purchase'); +Branch.userCompletedAction('complete_purchase').then(function (res) { + console.log(res); +}).catch(function (err) { + console.error(err); +}); -Branch.userCompletedAction('registered', { user: 'Test' }); +Branch.userCompletedAction('registered', { user: 'Test' }).then(function (res) { + console.log(res); +}).catch(function (err) { + console.error(err); +}); ``` ------ @@ -438,33 +456,33 @@ branchUniversalObj.onShareSheetLaunched(function () { }); ``` -###### onShareSheetDismissed +###### onShareSheetDismissed -The event fires when the share sheet is dismissed. +The event fires when the share sheet is dismissed. -```js -branchUniversalObj.onShareSheetDismissed(function () { - console.log('Share sheet dimissed'); -}); -``` +```js +branchUniversalObj.onShareSheetDismissed(function () { + console.log('Share sheet dimissed'); +}); +``` -###### onLinkShareResponse +###### onLinkShareResponse -The event returns a dictionary of the response data. +The event returns a dictionary of the response data. -```js -branchUniversalObj.onLinkShareResponse(function (res) { - console.log('Share link response: ' + JSON.stringify(res)); -}); -``` +```js +branchUniversalObj.onLinkShareResponse(function (res) { + console.log('Share link response: ' + JSON.stringify(res)); +}); +``` -###### onChannelSelected +###### onChannelSelected -The event fires when a channel is selected. +The event fires when a channel is selected. -```js -branchUniversalObj.onChannelSelected(function (res) { - console.log('Channel selected: ' + JSON.stringify(res)); +```js +branchUniversalObj.onChannelSelected(function (res) { + console.log('Channel selected: ' + JSON.stringify(res)); }); ``` diff --git a/src/android/io/branch/BranchSDK.java b/src/android/io/branch/BranchSDK.java index d56b080b..88259c35 100644 --- a/src/android/io/branch/BranchSDK.java +++ b/src/android/io/branch/BranchSDK.java @@ -787,6 +787,10 @@ public void onInitFinished(JSONObject referringParams, BranchError error) { Log.d(LCAT, "SessionListener onInitFinished()"); + String out = String.format("DeepLinkHandler(%s)", referringParams.toString()); + + webView.sendJavascript(out); + if (error == null) { // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app @@ -798,25 +802,15 @@ public void onInitFinished(JSONObject referringParams, BranchError error) { Log.d(LCAT, "return is not null"); Log.d(LCAT, referringParams.toString()); } - - String out = String.format("DeepLinkHandler(%s)", referringParams.toString()); - - webView.sendJavascript(out); - if (this._callbackContext != null) { - this._callbackContext.success(referringParams); - } + + this._callbackContext.success(referringParams); } else { String errorMessage = error.getMessage(); Log.d(LCAT, errorMessage); - String out = String.format("DeepLinkHandler(%s)", referringParams.toString()); - - webView.sendJavascript(out); - if (this._callbackContext != null) { - this._callbackContext.error(errorMessage); - } + this._callbackContext.error(errorMessage); } } diff --git a/src/ios/BranchSDK.h b/src/ios/BranchSDK.h index 9747ede7..b336534d 100644 --- a/src/ios/BranchSDK.h +++ b/src/ios/BranchSDK.h @@ -21,7 +21,7 @@ @property (copy) NSDictionary *metadata; @property (copy) NSDate *expirationDate; -@property (strong, nonatomic) BranchUniversalObject *branchUniversalObj; +@property (strong, nonatomic) NSMutableArray *branchUniversalObjArray; // BranchSDK Basic Methods - (void)initSession:(CDVInvokedUrlCommand*)command; @@ -41,9 +41,6 @@ // Branch Universal Object Methods - (void)createBranchUniversalObject:(CDVInvokedUrlCommand*)command; -- (void)initWithCanonicalIdentifier:(CDVInvokedUrlCommand*)command; -- (void)initWithTitle:(CDVInvokedUrlCommand*)command; -- (void)addMetadata:(CDVInvokedUrlCommand*)command; - (void)registerView:(CDVInvokedUrlCommand*)command; - (void)generateShortUrl:(CDVInvokedUrlCommand*)command; - (void)showShareSheet:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/BranchSDK.m b/src/ios/BranchSDK.m index fdff1623..5f10ad7e 100644 --- a/src/ios/BranchSDK.m +++ b/src/ios/BranchSDK.m @@ -9,6 +9,12 @@ @implementation BranchSDK +- (void)pluginInitialize +{ + NSLog(@"start pluginInitialize"); + self.branchUniversalObjArray = [[NSMutableArray alloc] init]; +} + #pragma mark - Private APIs #pragma mark - Global Instance Accessors @@ -73,34 +79,45 @@ - (void)initSession:(CDVInvokedUrlCommand*)command [branch initSessionWithLaunchOptions:nil andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) { NSLog(@"inside initSessionAndRegisterDeepLinkHandler block"); - NSString *resultString; + 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) { - NSError *err; - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params - options:0 - error:&err]; - if (!jsonData) { - NSLog(@"Parsing Error: %@", [err localizedDescription]); - resultString = [NSString stringWithFormat:@"Parsing Error: %@", [err localizedDescription]]; - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:resultString]; - } else { - NSLog(@"Success"); - resultString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params]; + NSLog(@"Success"); + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params]; + if (isFromUniversalLink) { + NSError *err; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"params":params} + options:0 + error:&err]; + if (!jsonData) { + NSLog(@"Parsing Error: %@", [err localizedDescription]); + NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:[err localizedDescription], @"error", nil]; + NSData* errorJSON = [NSJSONSerialization dataWithJSONObject:errorDict + options:NSJSONWritingPrettyPrinted + error:&err]; + + resultString = [[NSString alloc] initWithData:errorJSON encoding:NSUTF8StringEncoding]; + } else { + NSLog(@"Success"); + resultString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + } } } else { NSLog(@"No data found"); - resultString = @"No data found"; - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:resultString]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:FALSE]; } } 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]; + NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:[error localizedDescription], @"error", nil]; NSData* errorJSON = [NSJSONSerialization dataWithJSONObject:errorDict options:NSJSONWritingPrettyPrinted error:&error]; @@ -109,33 +126,37 @@ - (void)initSession:(CDVInvokedUrlCommand*)command pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:resultString]; } NSLog(@"returning data to js interface.."); - if (command != nil) { - NSLog(@"Sending to JS: %@", [params description]); - [self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId]; + + if (isFromUniversalLink) { + NSLog(@"Sending to DeepLinkHandler: %@", resultString); + [self.commandDelegate evalJs:[NSString stringWithFormat:@"DeepLinkHandler(%@)", resultString]]; } else { - NSLog(@"Command is nil"); + if (command != nil) { + NSLog(@"Sending to JS: %@", [params description]); + [self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId]; + } else { + NSLog(@"Command is nil"); + } } - [self.commandDelegate evalJs:[NSString stringWithFormat:@"DeepLinkHandler(%@)", resultString]]; }]; } - (void)setDebug:(CDVInvokedUrlCommand*)command { NSLog(@"start setDebug"); - CDVPluginResult* pluginResult; bool enableDebug = [[command.arguments objectAtIndex:0] boolValue] == YES; if (enableDebug) { [[Branch getInstance] setDebug]; } - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:enableDebug]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:enableDebug]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - (void)getAutoInstance:(CDVInvokedUrlCommand*)command { - NSLog(@"start setDebug"); + NSLog(@"start getAutoInstance"); [self initSession:nil]; } @@ -186,22 +207,11 @@ - (void)setIdentity:(CDVInvokedUrlCommand*)command NSLog(@"inside setIdentity block"); CDVPluginResult* pluginResult = nil; if (!error) { - NSError *err; - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params - options:0 - error:&err]; - if (!jsonData) { - NSLog(@"Parsing Error: %@", [err localizedDescription]); - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]]; - } else { - NSLog(@"Success"); - NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString]; - } + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params]; } else { NSLog(@"No data found"); - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Empty data"]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]]; } NSLog(@"returning data to js interface.."); [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -251,13 +261,13 @@ - (void)logout:(CDVInvokedUrlCommand*)command [branch logoutWithCallback:^(BOOL changed, NSError *error) { CDVPluginResult *pluginResult = nil; if (!error) { - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Success"]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:changed]; } else { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]]; } [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }]; - self.branchUniversalObj = nil; + self.branchUniversalObjArray = nil; } #pragma mark - Branch Referral Reward System @@ -299,19 +309,7 @@ - (void)redeemRewards:(CDVInvokedUrlCommand*)command NSLog(@"inside redeemRewards:forBucket block"); CDVPluginResult* pluginResult = nil; if (!error) { - NSNumber *changedValue = [NSNumber numberWithBool:changed]; - NSError *err; - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"changed":changedValue} - options:0 - error:&err]; - if (!jsonData) { - NSLog(@"Parsing Error: %@", [err localizedDescription]); - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]]; - } else { - NSLog(@"Success"); - NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString]; - } + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:changed]; } else { NSLog(@"Redeem Rewards Error: %@", [error localizedDescription]); @@ -325,19 +323,7 @@ - (void)redeemRewards:(CDVInvokedUrlCommand*)command NSLog(@"inside redeemRewards block"); CDVPluginResult* pluginResult = nil; if (!error) { - NSNumber *changedValue = [NSNumber numberWithBool:changed]; - NSError *err; - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"changed":changedValue} - options:0 - error:&err]; - if (!jsonData) { - NSLog(@"Parsing Error: %@", [err localizedDescription]); - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]]; - } else { - NSLog(@"Success"); - NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString]; - } + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:changed]; } else { NSLog(@"Redeem Rewards Error: %@", [error localizedDescription]); @@ -347,8 +333,6 @@ - (void)redeemRewards:(CDVInvokedUrlCommand*)command [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }]; } - - } - (void)getCreditHistory:(CDVInvokedUrlCommand*)command @@ -379,91 +363,69 @@ - (void)createBranchUniversalObject:(CDVInvokedUrlCommand*)command NSLog(@"start createBranchUniversalObject"); NSDictionary *properties = [command.arguments objectAtIndex:0]; - self.branchUniversalObj = [[BranchUniversalObject alloc] init]; + BranchUniversalObject *branchUniversalObj = [[BranchUniversalObject alloc] init]; for (id key in properties) { if ([key isEqualToString:@"contentMetadata"]){ NSDictionary *metadata = (NSDictionary *)[properties valueForKey:key]; for (id key_ in metadata) { - [self.branchUniversalObj addMetadataKey:key_ value:[metadata valueForKey:key_]]; + [branchUniversalObj addMetadataKey:key_ value:[metadata valueForKey:key_]]; } } else if ([key isEqualToString:@"contentIndexingMode"]) { NSString *indexingMode = [properties valueForKey:key]; + // Default contentIndexMode is always public if ([indexingMode isEqualToString:@"private"]) { - self.branchUniversalObj.contentIndexMode = ContentIndexModePrivate; - + branchUniversalObj.contentIndexMode = ContentIndexModePrivate; } - else if ([indexingMode isEqualToString:@"public"]){ - self.branchUniversalObj.contentIndexMode = ContentIndexModePublic; + else { + branchUniversalObj.contentIndexMode = ContentIndexModePublic; } } else if ([key isEqualToString:@"contentImageUrl"]){ NSString *imageUrl = [properties valueForKey:key]; - self.branchUniversalObj.imageUrl = imageUrl; + branchUniversalObj.imageUrl = imageUrl; } else { - [self.branchUniversalObj setValue:[properties objectForKey:key] forKey:key]; + [branchUniversalObj setValue:[properties objectForKey:key] forKey:key]; } } - NSLog(@"init BUO - %@", self.branchUniversalObj); - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"createBranchUniversalObject Success"]; - NSLog(@"returning data to js interface.."); - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; -} - -- (void)initWithCanonicalIdentifier:(CDVInvokedUrlCommand*)command -{ - NSLog(@"start initWithCanonicalIdentifier"); - if (!self.branchUniversalObj) { - self.branchUniversalObj = [[BranchUniversalObject alloc] initWithCanonicalIdentifier:[command.arguments objectAtIndex:0]]; - } - else { - self.branchUniversalObj.canonicalIdentifier = [command.arguments objectAtIndex:0]; - } - - NSLog(@"init BUO - %@", self.branchUniversalObj); - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"createBranchUniversalObject Success"]; - NSLog(@"returning data to js interface.."); - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; -} - -- (void)initWithTitle:(CDVInvokedUrlCommand*)command -{ - NSLog(@"start initWithTitle"); - if (!self.branchUniversalObj){ - self.branchUniversalObj = [[BranchUniversalObject alloc] initWithTitle:[command.arguments objectAtIndex:0]]; - } - else { - self.branchUniversalObj.title = [command.arguments objectAtIndex:0]; - } - - NSLog(@"init BUO - %@", self.branchUniversalObj); - CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"createBranchUniversalObject Success"]; + NSLog(@"init BUO - %@", branchUniversalObj); + [self.branchUniversalObjArray addObject:branchUniversalObj]; + 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]; } -- (void)addMetadata:(CDVInvokedUrlCommand*)command -{ - NSLog(@"start addMetadata"); - NSDictionary *metadata = [command.arguments objectAtIndex:0]; - [self.branchUniversalObj addMetadataKey:[metadata objectForKey:@"key"] value:[metadata objectForKey:@"value"]]; -} - - (void)registerView:(CDVInvokedUrlCommand*)command { NSLog(@"start registerView"); - [self.branchUniversalObj 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.."); + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; } - (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]; @@ -480,7 +442,7 @@ - (void)generateShortUrl:(CDVInvokedUrlCommand*)command [props addControlParam:key withValue:[arg1 objectForKey:key]]; } - [self.branchUniversalObj getShortUrlWithLinkProperties:props andCallback:^(NSString *url, NSError *error) { + [branchUniversalObj getShortUrlWithLinkProperties:props andCallback:^(NSString *url, NSError *error) { NSLog(@"inside getShortUrlWithLinkProperties block"); CDVPluginResult* pluginResult = nil; if (!error) { @@ -511,9 +473,12 @@ - (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]; @@ -530,7 +495,7 @@ - (void)showShareSheet:(CDVInvokedUrlCommand*)command [linkProperties addControlParam:key withValue:[arg1 objectForKey:key]]; } - [self.branchUniversalObj showShareSheetWithLinkProperties:linkProperties + [branchUniversalObj showShareSheetWithLinkProperties:linkProperties andShareText:shareText fromViewController:self.viewController completion:^(NSString *activityType, BOOL completed) { @@ -539,7 +504,10 @@ - (void)showShareSheet:(CDVInvokedUrlCommand*)command } - (void)listOnSpotlight:(CDVInvokedUrlCommand*)command { - [self.branchUniversalObj listOnSpotlightWithCallback:^(NSString *string, NSError *error) { + NSLog(@"start listOnSpotlight"); + int branchUniversalObjectId = [[command.arguments objectAtIndex:0] intValue]; + BranchUniversalObject *branchUniversalObj = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId]; + [branchUniversalObj listOnSpotlightWithCallback:^(NSString *string, NSError *error) { NSLog(@"inside listOnSpotlightWithCallback block"); CDVPluginResult* pluginResult = nil; if (!error) { diff --git a/testbed/www/js/index.js b/testbed/www/js/index.js index e62a3ee5..a53e067e 100644 --- a/testbed/www/js/index.js +++ b/testbed/www/js/index.js @@ -55,7 +55,11 @@ app.initialize(); function DeepLinkHandler(data) { - alert('Initialize: ' + JSON.stringify(data)); + if (data) { + alert('Initialize: ' + JSON.stringify(data)); + } else { + alert('No data found'); + } } function SetDebug(isEnabled) @@ -71,7 +75,13 @@ function InitSession() { console.log('Trigger InitSession()'); - Branch.initSession(); + Branch.initSession().then(function (res) { + console.log(res); + alert('Response: ' + JSON.stringify(res)); + }).catch(function (err) { + console.error(err); + alert('Error: ' + JSON.stringify(err)); + }); } function CustomAction() @@ -80,9 +90,13 @@ function CustomAction() var action = document.getElementById('custom-action').value; - Branch.userCompletedAction(action); - - alert('Success'); + Branch.userCompletedAction(action).then(function (res) { + console.log(res); + alert('Response: ' + JSON.stringify(res)); + }).catch(function (err) { + console.error(err); + alert('Error: ' + JSON.stringify(err)); + }); } function GetLatestReferringParams() @@ -91,7 +105,7 @@ function GetLatestReferringParams() Branch.getLatestReferringParams().then(function (res) { console.log(res); - alert('Success: ' + JSON.stringify(res)); + alert('Response: ' + JSON.stringify(res)); }).catch(function (err) { console.error(err); alert('Error: ' + JSON.stringify(err)); @@ -103,7 +117,7 @@ function GetFirstReferringParams() console.log('Trigger GetFirstReferringParams()'); Branch.getFirstReferringParams().then(function (res) { - alert('Success: ' + JSON.stringify(res)); + alert('Response: ' + JSON.stringify(res)); console.log(res); }).catch(function (err) { alert('Error: ' + JSON.stringify(err)); @@ -119,7 +133,7 @@ function SetIdentity() Branch.setIdentity(newIdentity).then(function (res) { console.log(res); - alert('Success: ' + JSON.stringify(res)); + alert('Response: ' + JSON.stringify(res)); }).catch(function (err) { console.error(err); alert('Error: ' + JSON.stringify(err)); @@ -130,9 +144,13 @@ function Logout() { console.log('Trigger Logout()'); - Branch.logout(); - - alert('Logout'); + Branch.logout().then(function (res) { + console.log(res); + alert('Response: ' + JSON.stringify(res)); + }).catch(function (err) { + console.error(err); + alert('Error: ' + JSON.stringify(err)); + }); } var branchUniversalObj = null; @@ -154,7 +172,7 @@ function CreateBranchUniversalObject() Branch.createBranchUniversalObject(properties) .then(function (res) { console.log(res); - alert('Success: ' + JSON.stringify(res)); + alert('Response: ' + JSON.stringify(res)); branchUniversalObj = res; }) .catch(function (err) { @@ -168,9 +186,13 @@ function RegisterView() { console.log('Trigger RegisterView()'); - branchUniversalObj.registerView(); - - alert('Success'); + branchUniversalObj.registerView().then(function (res) { + console.log(res); + alert('Response: ' + JSON.stringify(res)); + }).catch(function (err) { + console.error(err); + alert('Error: ' + JSON.stringify(err)); + });; } function GenerateShortUrl() @@ -197,12 +219,7 @@ function GenerateShortUrl() branchUniversalObj.generateShortUrl(properties, controlParams).then(function (res) { console.log(res); - if (navigator.userAgent.indexOf('iPhone') >= 0) { - var result = JSON.parse(res); - document.getElementById('generated-url').value = result.generatedLink; - } else if (navigator.userAgent.indexOf('Android') >= 0) { - document.getElementById('generated-url').value = res.url; - } + document.getElementById('generated-url').value = res.url; }).catch(function (err) { console.error(err); alert('Error: ' + JSON.stringify(err)); @@ -233,18 +250,18 @@ function ShowShareSheet() console.log(branchUniversalObj); - // Set listeners - branchUniversalObj.onShareSheetLaunched(function () { - console.log('Share sheet launched'); + // Set listeners + branchUniversalObj.onShareSheetLaunched(function () { + console.log('Share sheet launched'); }); - branchUniversalObj.onShareSheetDismissed(function () { - console.log('Share sheet dimissed'); + branchUniversalObj.onShareSheetDismissed(function () { + console.log('Share sheet dimissed'); }); - branchUniversalObj.onLinkShareResponse(function (res) { - console.log('Share link response: ' + JSON.stringify(res)); + branchUniversalObj.onLinkShareResponse(function (res) { + console.log('Share link response: ' + JSON.stringify(res)); }); - branchUniversalObj.onChannelSelected(function (res) { - console.log('Channel selected: ' + JSON.stringify(res)); + branchUniversalObj.onChannelSelected(function (res) { + console.log('Channel selected: ' + JSON.stringify(res)); }); branchUniversalObj.showShareSheet(properties, controlParams); @@ -256,7 +273,7 @@ function ListOnSpotlight() console.log('Trigger ListOnSpotlight()'); branchUniversalObj.listOnSpotlight().then(function (res) { console.log(res); - alert('Success: ' + JSON.stringify(res)); + alert('Response: ' + JSON.stringify(res)); }).catch(function (err) { console.error(err); alert('Error: ' + JSON.stringify(err)); @@ -268,7 +285,7 @@ function LoadRewards() console.log('Trigger LoadRewards()'); Branch.loadRewards().then(function (res) { console.log(res); - alert('Success: ' + JSON.stringify(res)); + alert('Response: ' + JSON.stringify(res)); }).catch(function (err) { console.error(err); alert('Error: ' + JSON.stringify(err)); @@ -282,7 +299,7 @@ function RedeemRewards() Branch.redeemRewards(reward).then(function (res) { console.log(res); - alert('Success: ' + JSON.stringify(res)); + alert('Response: ' + JSON.stringify(res)); }).catch(function (err) { console.error(err); alert('Error: ' + JSON.stringify(err)); @@ -294,7 +311,7 @@ function CreditHistory() console.log('Trigger CreditHistory()'); Branch.creditHistory().then(function (res) { console.log(res); - alert('Success: ' + JSON.stringify(res)); + alert('Response: ' + JSON.stringify(res)); }).catch(function (err) { console.error(err); alert('Error: ' + JSON.stringify(err)); diff --git a/www/branch.js b/www/branch.js index 9e5d1b18..4d050948 100644 --- a/www/branch.js +++ b/www/branch.js @@ -55,7 +55,7 @@ function executeCallback(method, callback, params) { * @class Branch */ var Branch = function () { - + this.debugMode = false; }; @@ -66,7 +66,7 @@ var Branch = function () { * @return (Promise) */ Branch.prototype.initSession = function () { - + return execute('initSession'); }; @@ -282,53 +282,53 @@ Branch.prototype.createBranchUniversalObject = function (options) { }; - /** - * Set on share sheet launched listener callback. - * - * @param (Function) callback + /** + * Set on share sheet launched listener callback. + * + * @param (Function) callback */ - obj.onShareSheetLaunched = function (callback) { + obj.onShareSheetLaunched = function (callback) { if (deviceVendor.indexOf('Apple') < 0) { executeCallback('onShareLinkDialogLaunched', callback, [obj.instanceId]); } - + }; obj.onShareSheetDismissed = function (callback) { - + if (deviceVendor.indexOf('Apple') < 0) { executeCallback('onShareLinkDialogDismissed', callback, [obj.instanceId]); } } - /** - * Set on link share listener callback. - * - * @param (Function) callback + /** + * Set on link share listener callback. + * + * @param (Function) callback */ - obj.onLinkShareResponse = function (callback) { - + obj.onLinkShareResponse = function (callback) { + if (deviceVendor.indexOf('Apple') < 0) { executeCallback('onLinkShareResponse', callback, [obj.instanceId]); } - + }; - - /** - * Set on channel select listener callback. - * - * @param (Function) callback + + /** + * Set on channel select listener callback. + * + * @param (Function) callback */ - obj.onChannelSelected = function (callback) { + obj.onChannelSelected = function (callback) { if (deviceVendor.indexOf('Apple') < 0) { executeCallback('onChannelSelected', callback, [obj.instanceId]); } - + }; - + /** * List item on Spotlight (iOS Only). */