Skip to content

Commit

Permalink
fix: updated ios sdk to 0.13.5
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanneff committed Mar 8, 2017
1 parent 37d7e40 commit e41ace2
Show file tree
Hide file tree
Showing 27 changed files with 668 additions and 249 deletions.
7 changes: 5 additions & 2 deletions src/ios/dependencies/Branch-SDK/BNCConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#include "BNCConfig.h"

#if defined(BNCTesting)
NSString * const BNC_API_BASE_URL = @"https://auhong.api.beta.branch.io";
#else
NSString * const BNC_API_BASE_URL = @"https://api.branch.io";
//NSString * const BNC_API_BASE_URL = @"https://ahmed.api.beta.branch.io";
#endif

NSString * const BNC_API_VERSION = @"v1";
NSString * const BNC_LINK_URL = @"https://bnc.lt";
NSString * const BNC_SDK_VERSION = @"0.12.27";
NSString * const BNC_SDK_VERSION = @"0.13.5";
2 changes: 2 additions & 0 deletions src/ios/dependencies/Branch-SDK/BNCDeviceInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@

//----------Methods----------------//
+ (BNCDeviceInfo *)getInstance;
+ (NSString*) userAgentString; // Warning: Has an implied lock on main thread on first call.
+ (NSString*) systemBuildVersion;

@end
115 changes: 98 additions & 17 deletions src/ios/dependencies/Branch-SDK/BNCDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <sys/sysctl.h>
#import "BNCDeviceInfo.h"
#import "BNCPreferenceHelper.h"
#import "BNCSystemObserver.h"
Expand Down Expand Up @@ -83,27 +84,107 @@ - (id)init {

}

self.browserUserAgent = [self.class userAgentString];
return self;
}

+ (NSString*) systemBuildVersion {
int mib[2] = { CTL_KERN, KERN_OSVERSION };
u_int namelen = sizeof(mib) / sizeof(mib[0]);

// Get the size for the buffer --

size_t bufferSize = 0;
sysctl(mib, namelen, NULL, &bufferSize, NULL, 0);
if (bufferSize <= 0) return nil;

u_char buildBuffer[bufferSize];
int result = sysctl(mib, namelen, buildBuffer, &bufferSize, NULL, 0);

NSString *version = nil;
if (result >= 0) {
version = [[NSString alloc]
initWithBytes:buildBuffer
length:bufferSize-1
encoding:NSUTF8StringEncoding];
}
return version;
}


+ (NSString*) userAgentString {

static NSString* browserUserAgentString = nil;
void (^setBrowserUserAgent)() = ^() {
if (!browserUserAgentString) {
browserUserAgentString =
[[[UIWebView alloc]
initWithFrame:CGRectZero]
stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
BNCPreferenceHelper *preferences = [BNCPreferenceHelper preferenceHelper];
preferences.browserUserAgentString = browserUserAgentString;
preferences.lastSystemBuildVersion = self.systemBuildVersion;
//NSLog(@"[Branch] userAgentString: '%@'.", browserUserAgentString);
}
};

// We only get the string once per app run:

if (browserUserAgentString)
return browserUserAgentString;

// Did we cache it?

BNCPreferenceHelper *preferences = [BNCPreferenceHelper preferenceHelper];
if (preferences.browserUserAgentString &&
preferences.lastSystemBuildVersion &&
[preferences.lastSystemBuildVersion isEqualToString:self.systemBuildVersion]) {
browserUserAgentString = [preferences.browserUserAgentString copy];
return browserUserAgentString;
}

void (^setUpBrowserUserAgent)() = ^() {
browserUserAgentString =
[[[UIWebView alloc]
initWithFrame:CGRectZero]
stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
self.browserUserAgent = browserUserAgentString;
};

@synchronized (self.class) {
if (browserUserAgentString) {
self.browserUserAgent = browserUserAgentString;
} else if (NSThread.isMainThread) {
setUpBrowserUserAgent();
} else {
dispatch_sync(dispatch_get_main_queue(), setUpBrowserUserAgent);
}
// Make sure this executes on the main thread.
// Uses an implied lock through dispatch_queues: This can deadlock if mis-used!

if (NSThread.isMainThread) {
setBrowserUserAgent();
return browserUserAgentString;
}

// Different case for iOS 7.0:
if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {
dispatch_sync(dispatch_get_main_queue(), ^ {
setBrowserUserAgent();
});
return browserUserAgentString;
}

return self;
// Wait and yield to prevent deadlock:

int retries = 10;
int64_t timeoutDelta = (dispatch_time_t)((long double)NSEC_PER_SEC * (long double)0.100);
while (!browserUserAgentString && retries > 0) {

dispatch_block_t agentBlock = dispatch_block_create_with_qos_class(
DISPATCH_BLOCK_DETACHED | DISPATCH_BLOCK_ENFORCE_QOS_CLASS,
QOS_CLASS_USER_INTERACTIVE,
0, ^ {
//NSLog(@"Will userAgent.");
setBrowserUserAgent();
//NSLog(@"Did userAgent.");
});
dispatch_async(dispatch_get_main_queue(), agentBlock);

dispatch_time_t timeoutTime = dispatch_time(DISPATCH_TIME_NOW, timeoutDelta);
#if defined(BNCTesting)
long result = dispatch_block_wait(agentBlock, timeoutTime);
NSLog(@"Wait result: %ld.", result);
#else
dispatch_block_wait(agentBlock, timeoutTime);
#endif
retries--;
}
return browserUserAgentString;
}

@end
14 changes: 9 additions & 5 deletions src/ios/dependencies/Branch-SDK/BNCLinkCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
// Copyright (c) 2015 Branch Metrics. All rights reserved.
//


#import "BNCLinkCache.h"

@interface BNCLinkCache ()

@interface BNCLinkCache ()
@property (nonatomic, strong) NSMutableDictionary *cache;

@end


@implementation BNCLinkCache

- (id)init {
if (self = [super init]) {
self.cache = [[NSMutableDictionary alloc] init];
}

return self;
}

- (void)setObject:(NSString *)anObject forKey:(BNCLinkData *)aKey {
self.cache[@([aKey hash])] = anObject;
@synchronized (self) {
self.cache[@([aKey hash])] = anObject;
}
}

- (NSString *)objectForKey:(BNCLinkData *)aKey {
return self.cache[@([aKey hash])];
@synchronized (self) {
return self.cache[@([aKey hash])];
}
}

@end
8 changes: 7 additions & 1 deletion src/ios/dependencies/Branch-SDK/BNCPreferenceHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@property (strong, nonatomic) NSString *branchKey;
@property (strong, nonatomic) NSString *lastRunBranchKey;
@property (strong, nonatomic) NSDate *lastStrongMatchDate;
@property (strong, nonatomic) NSDate *lastStrongMatchDate;
@property (strong, nonatomic) NSString *appVersion;
@property (strong, nonatomic) NSString *deviceFingerprintID;
@property (strong, nonatomic) NSString *sessionID;
Expand All @@ -39,6 +39,9 @@
@property (strong, nonatomic) NSMutableDictionary *savedAnalyticsData;
@property (assign, nonatomic) NSInteger installRequestDelay;
@property (strong, nonatomic) NSDictionary *appleSearchAdDetails;
@property (strong, nonatomic) NSString *lastSystemBuildVersion;
@property (strong, nonatomic) NSString *browserUserAgentString;
@property (strong) NSString *branchAPIURL;

+ (BNCPreferenceHelper *)preferenceHelper;
+ (NSURL*) URLForBranchDirectory;
Expand Down Expand Up @@ -79,4 +82,7 @@
- (NSMutableDictionary *)getBranchAnalyticsData;
- (NSDictionary *)getContentAnalyticsManifest;
- (void)saveContentAnalyticsManifest:(NSDictionary *)cdManifest;

- (void) save; // Flushes preference queue to persistence.

@end
95 changes: 88 additions & 7 deletions src/ios/dependencies/Branch-SDK/BNCPreferenceHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@
// 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 ()
@interface BNCPreferenceHelper () {
NSOperationQueue *_persistPrefsQueue;
NSString *_lastSystemBuildVersion;
NSString *_browserUserAgentString;
NSString *_branchAPIURL;
}

@property (strong, nonatomic) NSMutableDictionary *persistenceDict;
@property (strong, nonatomic) NSMutableDictionary *creditsDictionary;
Expand Down Expand Up @@ -121,6 +126,11 @@ + (BNCPreferenceHelper *)getInstance {
return preferenceHelper;
}


/*
This creates one global queue. Not so desirable.
- (NSOperationQueue *)persistPrefsQueue {
static NSOperationQueue *persistPrefsQueue;
static dispatch_once_t persistOnceToken;
Expand All @@ -132,6 +142,26 @@ - (NSOperationQueue *)persistPrefsQueue {
return persistPrefsQueue;
}
*/

- (NSOperationQueue *)persistPrefsQueue {
@synchronized (self) {
if (_persistPrefsQueue)
return _persistPrefsQueue;
_persistPrefsQueue = [[NSOperationQueue alloc] init];
_persistPrefsQueue.maxConcurrentOperationCount = 1;
return _persistPrefsQueue;
}
}

- (void) save {
// Flushes preference queue to persistence.
[_persistPrefsQueue waitUntilAllOperationsAreFinished];
}

- (void) dealloc {
[self save];
}

#pragma mark - Debug methods

Expand All @@ -151,18 +181,40 @@ - (void)logWarning:(NSString *)message {
}
}

- (void) setBranchAPIURL:(NSString*)branchAPIURL_ {
@synchronized (self) {
_branchAPIURL = [branchAPIURL_ copy];
}
}

- (NSString*) branchAPIURL {
@synchronized (self) {
if (!_branchAPIURL) {
_branchAPIURL = [BNC_API_BASE_URL copy];
}
return _branchAPIURL;
}
}

- (NSString *)getAPIBaseURL {
return [NSString stringWithFormat:@"%@/%@/", BNC_API_BASE_URL, BNC_API_VERSION];
@synchronized (self) {
return [NSString stringWithFormat:@"%@/%@/", self.branchAPIURL, BNC_API_VERSION];
}
}

- (NSString *)getAPIURL:(NSString *) endpoint {
return [[self getAPIBaseURL] stringByAppendingString:endpoint];
}

- (NSString *)getEndpointFromURL:(NSString *)url {
NSUInteger index = BNC_API_BASE_URL.length;
return [url substringFromIndex:index];
NSString *APIBase = self.branchAPIURL;
if ([url hasPrefix:APIBase]) {
NSUInteger index = APIBase.length;
return [url substringFromIndex:index];
}
return @"";
}

#pragma mark - Preference Storage

- (NSString *)getBranchKey:(BOOL)isLive {
Expand Down Expand Up @@ -434,6 +486,34 @@ - (NSDictionary*) appleSearchAdDetails {
return [_appleSearchAdDetails isKindOfClass:[NSDictionary class]] ? _appleSearchAdDetails : nil;
}

- (NSString*) lastSystemBuildVersion {
if (!_lastSystemBuildVersion) {
_lastSystemBuildVersion = [self readStringFromDefaults:@"_lastSystemBuildVersion"];
}
return _lastSystemBuildVersion;
}

- (void) setLastSystemBuildVersion:(NSString *)lastSystemBuildVersion {
if (![_lastSystemBuildVersion isEqualToString:lastSystemBuildVersion]) {
_lastSystemBuildVersion = lastSystemBuildVersion;
[self writeObjectToDefaults:@"_lastSystemBuildVersion" value:_lastSystemBuildVersion];
}
}

- (NSString*) browserUserAgentString {
if (!_browserUserAgentString) {
_browserUserAgentString = [self readStringFromDefaults:@"_browserUserAgentString"];
}
return _browserUserAgentString;
}

- (void) setBrowserUserAgentString:(NSString *)browserUserAgentString {
if (![_browserUserAgentString isEqualToString:browserUserAgentString]) {
_browserUserAgentString = browserUserAgentString;
[self writeObjectToDefaults:@"_browserUserAgentString" value:_browserUserAgentString];
}
}

- (NSString *)userUrl {
if (!_userUrl) {
_userUrl = [self readStringFromDefaults:BRANCH_PREFS_KEY_USER_URL];
Expand Down Expand Up @@ -672,10 +752,10 @@ - (void)persistPrefsToDisk {
[self logWarning:@"Can't create preferences data."];
return;
}
NSURL *prefsURL = self.class.URLForPrefsFile;
NSBlockOperation *newPersistOp = [NSBlockOperation blockOperationWithBlock:^ {
NSError *error = nil;
[data writeToURL:self.class.URLForPrefsFile
options:NSDataWritingAtomic error:&error];
[data writeToURL:prefsURL options:NSDataWritingAtomic error:&error];
if (error) {
[self logWarning:
[NSString stringWithFormat:
Expand Down Expand Up @@ -810,7 +890,8 @@ + (NSURL* _Nonnull) URLForPrefsFile {
}

+ (void) moveOldPrefsFile {
NSURL *oldURL = [NSURL fileURLWithPath:self.prefsFile_deprecated];
NSString* oldPath = self.prefsFile_deprecated;
NSURL *oldURL = (oldPath) ? [NSURL fileURLWithPath:self.prefsFile_deprecated] : nil;
NSURL *newURL = [self URLForPrefsFile];

if (!oldURL || !newURL) { return; }
Expand Down
Loading

0 comments on commit e41ace2

Please sign in to comment.