Skip to content

Commit

Permalink
Merge pull request #79 from pragunvohra/iosPromises
Browse files Browse the repository at this point in the history
Fixes for iOS plugin (& ensure consistency w/Android implementation)
  • Loading branch information
aaustin committed Mar 24, 2016
2 parents c2f8964 + ad8a615 commit 836cd61
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 64 deletions.
12 changes: 6 additions & 6 deletions src/android/io/branch/BranchSDK.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,15 @@ private void getLatestReferringParams(CallbackContext callbackContext)

JSONObject sessionParams = this.instance.getLatestReferringParams();

if (sessionParams == null) {
if (sessionParams == null || sessionParams.length() == 0) {
Log.d(LCAT, "return is null");
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, /* send boolean: false as the data */ false));
} else {
Log.d(LCAT, "return is not null");
Log.d(LCAT, sessionParams.toString());
callbackContext.success(sessionParams);
}

callbackContext.success(sessionParams);

}

/**
Expand All @@ -316,15 +316,15 @@ private void getFirstReferringParams(CallbackContext callbackContext)

JSONObject installParams = this.instance.getFirstReferringParams();

if (installParams == null) {
if (installParams == null || installParams.length() == 0) {
Log.d(LCAT, "return is null");
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, /* send boolean: false as the data */ false));
} else {
Log.d(LCAT, "return is not null");
Log.d(LCAT, installParams.toString());
callbackContext.success(installParams);
}

callbackContext.success(installParams);

}

/**
Expand Down
97 changes: 39 additions & 58 deletions src/ios/BranchSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
[branch initSessionWithLaunchOptions:nil andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) {
NSLog(@"inside initSessionAndRegisterDeepLinkHandler block");
NSString *resultString;
CDVPluginResult *pluginResult = nil;
if (!error) {
if (params != nil && [params count] > 0) {
NSError *err;
Expand All @@ -83,13 +84,16 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
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];
}
} else {
NSLog(@"No data found");
resultString = @"No data found";
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:resultString];
}
}
else {
Expand All @@ -102,8 +106,15 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
error:&error];

resultString = [[NSString alloc] initWithData:errorJSON encoding:NSUTF8StringEncoding];
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];
} else {
NSLog(@"Command is nil");
}
[self.commandDelegate evalJs:[NSString stringWithFormat:@"DeepLinkHandler(%@)", resultString]];
}];
}
Expand All @@ -129,21 +140,11 @@ - (void)getLatestReferringParams:(CDVInvokedUrlCommand*)command
CDVPluginResult* pluginResult = nil;

if (sessionParams != nil && [sessionParams count] > 0) {
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sessionParams
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];
}
NSLog(@"Success");
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:sessionParams];
} else {
NSLog(@"No data found");
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Empty data"];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:FALSE];
}
NSLog(@"returning data to js interface..");
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand All @@ -158,21 +159,11 @@ - (void)getFirstReferringParams:(CDVInvokedUrlCommand*)command
CDVPluginResult* pluginResult = nil;

if (installParams != nil && [installParams count] > 0) {
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:installParams
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];
}
NSLog(@"Success");
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:installParams];
} else {
NSLog(@"No data found");
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Empty data"];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:FALSE];
}
NSLog(@"returning data to js interface..");
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand Down Expand Up @@ -239,13 +230,25 @@ - (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];
}

- (void)logout:(CDVInvokedUrlCommand*)command
{
NSLog(@"start logout");
Branch *branch = [self getInstance];
[branch logout];
[branch logoutWithCallback:^(BOOL changed, NSError *error) {
CDVPluginResult *pluginResult = nil;
if (!error) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Success"];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
self.branchUniversalObj = nil;
}

Expand All @@ -260,19 +263,9 @@ - (void)loadRewards:(CDVInvokedUrlCommand*)command
NSLog(@"inside loadRewardsWithCallback block");
CDVPluginResult* pluginResult = nil;
if(!error) {
NSNumber *credits = [NSNumber numberWithInteger:[branch getCredits]];
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"credits":credits}
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];
}
int credits = (int)[branch getCredits];
NSLog(@"Success");
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:credits];
}
else {
NSLog(@"Load Rewards Error: %@", [error localizedDescription]);
Expand Down Expand Up @@ -359,18 +352,8 @@ - (void)getCreditHistory:(CDVInvokedUrlCommand*)command
NSLog(@"inside getCreditHistoryWithCallback block");
CDVPluginResult* pluginResult = nil;
if (!error) {
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:list
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];
}
NSLog(@"Success");
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:list];
}
else {
NSLog(@"Credit History Error: %@", [error localizedDescription]);
Expand Down Expand Up @@ -494,16 +477,14 @@ - (void)generateShortUrl:(CDVInvokedUrlCommand*)command
CDVPluginResult* pluginResult = nil;
if (!error) {
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"generatedLink":url}
options:0
error:&err];
if (!jsonData) {
NSDictionary *jsonObj = [[NSDictionary alloc] initWithObjectsAndKeys:url, @"url", 0, @"options", &err, @"error", nil];

if (!jsonObj) {
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:jsonObj];
}
}
else {
Expand Down

0 comments on commit 836cd61

Please sign in to comment.