Skip to content

Commit

Permalink
updated 2.1.4 with Fabric Answers integration code for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
aaustin committed Jun 21, 2016
1 parent d36ff82 commit cd925e3
Show file tree
Hide file tree
Showing 32 changed files with 779 additions and 120 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.1.2",
"version": "2.1.4",
"name": "branch-cordova-sdk",
"description": "Branch Metrics Cordova SDK",
"main": "www/branch.js",
Expand Down
16 changes: 13 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SOFTWARE.
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="io.branch.sdk"
version="2.1.2">
version="2.1.4">

<name>branch-cordova-sdk</name>
<description>Branch SDK Plugin</description>
Expand Down Expand Up @@ -101,14 +101,13 @@ SOFTWARE.
</array>
</config-file>
<header-file src="src/ios/BranchSDK.h" />
<header-file src="src/ios/dependencies/Branch-SDK/BranchView.h" />
<header-file src="src/ios/dependencies/Branch-SDK/BranchViewHandler.h" />
<source-file src="src/ios/BranchSDK.m" />
<source-file src="src/ios/AppDelegate+BranchSdk.m" />
<!-- COMMENT if building for NPM -->
<framework custom="true" src="src/ios/dependencies/Branch.framework" />
<!-- UNCOMMENT if building for NPM -->
<!-- <header-file src="src/ios/dependencies/Branch-SDK/BNCConfig.h" />
<header-file src="src/ios/dependencies/Branch-SDK/BNCCallbacks.h" />
<header-file src="src/ios/dependencies/Branch-SDK/BNCContentDiscoveryManager.h" />
<source-file src="src/ios/dependencies/Branch-SDK/BNCContentDiscoveryManager.m" />
<header-file src="src/ios/dependencies/Branch-SDK/BNCEncodingUtils.h" />
Expand Down Expand Up @@ -149,6 +148,17 @@ SOFTWARE.
<source-file src="src/ios/dependencies/Branch-SDK/BranchView.m" />
<header-file src="src/ios/dependencies/Branch-SDK/BranchViewHandler.h" />
<source-file src="src/ios/dependencies/Branch-SDK/BranchViewHandler.m" />
<header-file src="src/ios/dependencies/Branch-SDK/BNCFabricAnswers.h" />
<source-file src="src/ios/dependencies/Branch-SDK/BNCFabricAnswers.m" />
<header-file src="src/ios/dependencies/Branch-SDK/BNCDeviceInfo.h" />
<source-file src="src/ios/dependencies/Branch-SDK/BNCDeviceInfo.m" />
<header-file src="src/ios/dependencies/Fabric/ANSCompatibility.h" />
<header-file src="src/ios/dependencies/Fabric/Answers.h" />
<header-file src="src/ios/dependencies/Fabric/FABAttributes.h" />
<header-file src="src/ios/dependencies/Fabric/FABKitProtocol.h" />
<header-file src="src/ios/dependencies/Fabric/Fabric.h" />
<header-file src="src/ios/dependencies/Fabric/Fabric+FABKits.h" />
<header-file src="src/ios/dependencies/Branch-SDK/Requests/BNCServerRequest.h" />
<source-file src="src/ios/dependencies/Branch-SDK/Requests/BNCServerRequest.m" />
Expand Down
21 changes: 21 additions & 0 deletions src/ios/dependencies/Branch-SDK/BNCCallbacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// BNCCallbacks.h
// Branch-TestBed
//
// Created by Ahmed Nawar on 6/18/16.
// Copyright © 2016 Branch Metrics. All rights reserved.
//

#ifndef BNCCallbacks_h
#define BNCCallbacks_h

@class BranchUniversalObject, BranchLinkProperties;

typedef void (^callbackWithParams) (NSDictionary *params, NSError *error);
typedef void (^callbackWithUrl) (NSString *url, NSError *error);
typedef void (^callbackWithStatus) (BOOL changed, NSError *error);
typedef void (^callbackWithList) (NSArray *list, NSError *error);
typedef void (^callbackWithUrlAndSpotlightIdentifier) (NSString *url, NSString *spotlightIdentifier, NSError *error);
typedef void (^callbackWithBranchUniversalObject) (BranchUniversalObject *universalObject, BranchLinkProperties *linkProperties, NSError *error);

#endif /* BNCCallbacks_h */
2 changes: 1 addition & 1 deletion src/ios/dependencies/Branch-SDK/BNCConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef Branch_SDK_Config_h
#define Branch_SDK_Config_h

#define SDK_VERSION @"0.12.2"
#define SDK_VERSION @"0.12.3"

#define BNC_PROD_ENV
//#define BNC_STAGE_ENV
Expand Down
16 changes: 16 additions & 0 deletions src/ios/dependencies/Branch-SDK/BNCFabricAnswers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// BNCFabricAnswers.h
// Branch-TestBed
//
// Created by Ahmed Nawar on 6/2/16.
// Copyright © 2016 Branch Metrics. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BNCFabricAnswers : NSObject

+ (void)sendEventWithName:(NSString*)name andAttributes:(NSDictionary*)attributes;
+ (NSDictionary *)prepareBranchDataForAnswers:(NSDictionary *)dictionary;

@end
51 changes: 51 additions & 0 deletions src/ios/dependencies/Branch-SDK/BNCFabricAnswers.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// BNCFabricAnswers.m
// Branch-TestBed
//
// Created by Ahmed Nawar on 6/2/16.
// Copyright © 2016 Branch Metrics. All rights reserved.
//

#import "BNCFabricAnswers.h"
#import "BNCPreferenceHelper.h"
#import "../Fabric/Answers.h"

@implementation BNCFabricAnswers

+ (void)sendEventWithName:(NSString *)name andAttributes:(NSDictionary *)attributes {
ANSLogCustomEvent(name, [self prepareBranchDataForAnswers:attributes]);
}

+ (NSDictionary *)prepareBranchDataForAnswers:(NSDictionary *)dictionary {
NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];

for (NSString *key in dictionary.allKeys) {
if ([key hasPrefix:@"+"] || ([key hasPrefix:@"$"] && ![key isEqualToString:@"$identity_id"])) {
// ignore because this data is not found on the ShareSheet
continue;
} else if ([dictionary[key] isKindOfClass:[NSArray class]]) {
// flatten arrays, special treatement for ~tags
NSString *aKey;
if ([key hasPrefix:@"~"])
aKey = [key substringFromIndex:1];
else
aKey = key;
NSArray *valuesArray = dictionary[key];
for (NSUInteger i = 0; i < valuesArray.count; ++i) {
temp[[NSString stringWithFormat:@"%@.%lu", aKey, (unsigned long)i]] = valuesArray[i];
}
} else if ([key hasPrefix:@"~"]) {
// strip tildes ~
temp[[key substringFromIndex:1]] = dictionary[key];
} else if ([key isEqualToString:@"$identity_id"]) {
temp[@"referring_branch_identity"] = dictionary[key];
}
}

BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper preferenceHelper];
temp[@"branch_identity"] = preferenceHelper.identityID;

return temp;
}

@end
15 changes: 15 additions & 0 deletions src/ios/dependencies/Branch-SDK/BNCPreferenceHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "BNCEncodingUtils.h"
#import "BNCConfig.h"
#import "Branch.h"
#import "../Fabric/Fabric+FABKits.h"

static const NSTimeInterval DEFAULT_TIMEOUT = 5.5;
static const NSTimeInterval DEFAULT_RETRY_INTERVAL = 0;
Expand Down Expand Up @@ -45,6 +46,9 @@

NSString * const BRANCH_PREFS_KEY_BRANCH_VIEW_USAGE_CNT = @"bnc_branch_view_usage_cnt_";

// The name of this key was specified in the account-creation API integration
static NSString * const BNC_BRANCH_FABRIC_APP_KEY_KEY = @"branch_key";

@interface BNCPreferenceHelper ()

@property (strong, nonatomic) NSMutableDictionary *persistenceDict;
Expand Down Expand Up @@ -193,6 +197,17 @@ - (NSString *)getBranchKey:(BOOL)isLive {
else if ([ret isKindOfClass:[NSDictionary class]]) {
self.branchKey = isLive ? ret[@"live"] : ret[@"test"];
}
} else {
Class fabric = NSClassFromString(@"Fabric");

if (fabric) {
NSDictionary *configDictionary = [fabric configurationDictionaryForKitClass:[Branch class]];
ret = [configDictionary objectForKey:BNC_BRANCH_FABRIC_APP_KEY_KEY];

if ([ret isKindOfClass:[NSString class]]) {
self.branchKey = ret;
}
}
}

return _branchKey;
Expand Down
6 changes: 5 additions & 1 deletion src/ios/dependencies/Branch-SDK/BNCServerInterface.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ - (void)genericHTTPRequest:(NSURLRequest *)request retryNumber:(NSInteger)retryN
NSURLSessionCompletionHandler = ^void(NSData *data, NSURLResponse *response, NSError *error) {
BNCServerResponse *serverResponse = [self processServerResponse:response data:data error:error log:log];
NSInteger status = [serverResponse.statusCode integerValue];
BOOL isRetryableStatusCode = status >= 500;
// If the phone is in a poor network condition,
// iOS will return statuses such as -1001, -1003, -1200, -9806
// indicating various parts of the HTTP post failed.
// We should retry in those conditions in addition to the case where the server returns a 500
BOOL isRetryableStatusCode = status >= 500 || status < 0;

// Retry the request if appropriate
if (retryNumber < self.preferenceHelper.retryCount && isRetryableStatusCode) {
Expand Down
21 changes: 9 additions & 12 deletions src/ios/dependencies/Branch-SDK/Branch.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,25 @@
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "BranchActivityItemProvider.h"
#import "BNCConfig.h"
#import "BranchView.h"
#import "BNCCallbacks.h"
#import "BNCLinkCache.h"
#import "BranchViewHandler.h"
#import "BNCServerInterface.h"
#import "BNCPreferenceHelper.h"
#import "BranchLinkProperties.h"
#import "BranchUniversalObject.h"
#import "BNCServerRequestQueue.h"
#import "BNCLinkCache.h"
#import "BranchActivityItemProvider.h"
#import "BranchDeepLinkingController.h"
#import "BNCPreferenceHelper.h"
@class BranchUniversalObject;
@class BranchLinkProperties;

/**
`Branch` is the primary interface of the Branch iOS SDK. Currently, all interactions you will make are funneled through this class. It is not meant to be instantiated or subclassed, usage should be limited to the global instance.
Note, when `getInstance` is called, it assumes that you have already placed a Branch Key in your main `Info.plist` file for your project. For additional information on configuring the Branch SDK, check out the getting started guides in the Readme.
*/

typedef void (^callbackWithParams) (NSDictionary *params, NSError *error);
typedef void (^callbackWithUrl) (NSString *url, NSError *error);
typedef void (^callbackWithStatus) (BOOL changed, NSError *error);
typedef void (^callbackWithList) (NSArray *list, NSError *error);
typedef void (^callbackWithUrlAndSpotlightIdentifier) (NSString *url, NSString *spotlightIdentifier, NSError *error);
typedef void (^callbackWithBranchUniversalObject) (BranchUniversalObject *universalObject, BranchLinkProperties *linkProperties, NSError *error);

///----------------
/// @name Constants
///----------------
Expand Down
4 changes: 1 addition & 3 deletions src/ios/dependencies/Branch-SDK/Branch.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#import "../Fabric/FABKitProtocol.h"
#import "../Fabric/Fabric+FABKits.h"


NSString * const BRANCH_FEATURE_TAG_SHARE = @"share";
NSString * const BRANCH_FEATURE_TAG_REFERRAL = @"referral";
NSString * const BRANCH_FEATURE_TAG_INVITE = @"invite";
Expand Down Expand Up @@ -326,7 +325,6 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)

- (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)isReferrable explicitlyRequestedReferrable:(BOOL)explicitlyRequestedReferrable automaticallyDisplayController:(BOOL)automaticallyDisplayController {
self.shouldAutomaticallyDeepLink = automaticallyDisplayController;

self.preferenceHelper.isReferrable = isReferrable;
self.preferenceHelper.explicitlyRequestedReferrable = explicitlyRequestedReferrable;

Expand Down Expand Up @@ -387,7 +385,7 @@ - (BOOL)handleDeepLink:(NSURL *)url {
}
}

[self initUserSessionAndCallCallback:YES];
[self initUserSessionAndCallCallback:!self.isInitialized];

return handled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ - (id)item {
if ([channel isEqualToString:@"facebook"] || [channel isEqualToString:@"twitter"] || [channel isEqualToString:@"com.tinyspeck.chatlyio.share"]) {
return [NSURL URLWithString:[[Branch getInstance] getShortURLWithParams:params andTags:tags andChannel:channel andFeature:feature andStage:stage andAlias:alias ignoreUAString:self.userAgentString forceLinkCreation:YES]];
}

return [NSURL URLWithString:[[Branch getInstance] getShortURLWithParams:params andTags:tags andChannel:channel andFeature:feature andStage:stage andAlias:alias ignoreUAString:nil forceLinkCreation:YES]];
}

Expand Down
15 changes: 0 additions & 15 deletions src/ios/dependencies/Branch-SDK/BranchSDK.h

This file was deleted.

3 changes: 2 additions & 1 deletion src/ios/dependencies/Branch-SDK/BranchUniversalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
//

#import <Foundation/Foundation.h>
#import "BranchLinkProperties.h"
#import "Branch.h"

@class BranchLinkProperties;

typedef void (^callback) ();
typedef void (^shareCompletion) (NSString *activityType, BOOL completed);

Expand Down
14 changes: 14 additions & 0 deletions src/ios/dependencies/Branch-SDK/BranchUniversalObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "BNCError.h"
#import "BranchConstants.h"
#import "BNCPreferenceHelper.h"
#import "BNCFabricAnswers.h"

@implementation BranchUniversalObject {
BNCPreferenceHelper *_preferenceHelper;
Expand Down Expand Up @@ -164,6 +165,7 @@ - (void)showShareSheetWithLinkProperties:(BranchLinkProperties *)linkProperties
shareViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (completion) {
completion(activityType, completed);
[BNCFabricAnswers sendEventWithName:@"Branch Share" andAttributes:[self getDictionaryWithCompleteLinkProperties:linkProperties]];
}
};
} else {
Expand Down Expand Up @@ -354,6 +356,18 @@ - (NSDictionary *)getParamsForServerRequestWithAddedLinkProperties:(BranchLinkPr
return [temp copy];
}

- (NSDictionary *)getDictionaryWithCompleteLinkProperties:(BranchLinkProperties *)linkProperties {
NSMutableDictionary *temp = [[self getParamsForServerRequestWithAddedLinkProperties:linkProperties] mutableCopy];

[self safeSetValue:linkProperties.tags forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_TAGS] onDict:temp];
[self safeSetValue:linkProperties.feature forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_FEATURE] onDict:temp];
[self safeSetValue:linkProperties.alias forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_ALIAS] onDict:temp];
[self safeSetValue:linkProperties.channel forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_CHANNEL] onDict:temp];
[self safeSetValue:linkProperties.stage forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_STAGE] onDict:temp];
[self safeSetValue:@(linkProperties.matchDuration) forKey:[NSString stringWithFormat:@"~%@", BRANCH_REQUEST_KEY_URL_DURATION] onDict:temp];

return [temp copy];
}
- (void)safeSetValue:(NSObject *)value forKey:(NSString *)key onDict:(NSMutableDictionary *)dict {
if (value) {
dict[key] = value;
Expand Down
12 changes: 8 additions & 4 deletions src/ios/dependencies/Branch-SDK/Requests/BranchOpenRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "BranchConstants.h"
#import "BNCEncodingUtils.h"
#import "BranchViewHandler.h"
#import "BNCFabricAnswers.h"

@interface BranchOpenRequest ()

Expand Down Expand Up @@ -80,32 +81,35 @@ - (void)processResponse:(BNCServerResponse *)response error:(NSError *)error {
preferenceHelper.userUrl = data[BRANCH_RESPONSE_KEY_USER_URL];
preferenceHelper.userIdentity = userIdentity;
preferenceHelper.sessionID = data[BRANCH_RESPONSE_KEY_SESSION_ID];

[BNCSystemObserver setUpdateState];

NSString *sessionData = data[BRANCH_RESPONSE_KEY_SESSION_DATA];

// Update session params
preferenceHelper.sessionParams = sessionData;

// Scenarios:
// If no data, data isn't from a link click, or isReferrable is false, don't set, period.
// Otherwise,
// * On Install: set.
// * On Open and installParams set: don't set.
// * On Open and stored installParams are empty: set.
if (sessionData.length && preferenceHelper.isReferrable) {
if (sessionData.length) {
NSDictionary *sessionDataDict = [BNCEncodingUtils decodeJsonStringToDictionary:sessionData];
BOOL dataIsFromALinkClick = [sessionDataDict[BRANCH_RESPONSE_KEY_CLICKED_BRANCH_LINK] isEqual:@1];

BOOL storedParamsAreEmpty = YES;

if ([preferenceHelper.installParams isKindOfClass:[NSString class]]) {
storedParamsAreEmpty = !preferenceHelper.installParams.length;
}

if (dataIsFromALinkClick && (self.isInstall || storedParamsAreEmpty)) {
preferenceHelper.installParams = sessionData;
}

if (dataIsFromALinkClick) {
[BNCFabricAnswers sendEventWithName:[@"Branch " stringByAppendingString:[[self getActionName] capitalizedString]] andAttributes:sessionDataDict];
}
}

// Clear link identifiers so they don't get reused on the next open
Expand Down
Loading

0 comments on commit cd925e3

Please sign in to comment.