Skip to content

Commit 58530fd

Browse files
author
Edward Smith
committed
Merge branch 'QA' of ssh://github.com/BranchMetrics/ios-branch-deep-linking
2 parents 45bb63b + 418873f commit 58530fd

33 files changed

+691
-288
lines changed

.gitignore

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
Pods/
2-
carthage-files/*.zip
3-
carthage-files/Carthage
1+
# Mac
2+
.DS_Store
3+
.LSOverride
4+
5+
# Xcode
6+
[Bb]uild
47
*.xcuserstate
58
*.xcuserdatad
6-
*.DS_Store
79
*.xcscmblueprint
10+
*.o
11+
12+
# Pods
13+
Pods/
814

915
# Carthage
1016
Carthage/Build
17+
carthage-files/*.zip
18+
carthage-files/Carthage
19+

Branch-SDK/Branch-SDK/BNCConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef Branch_SDK_Config_h
1010
#define Branch_SDK_Config_h
1111

12-
#define SDK_VERSION @"0.12.12"
12+
#define SDK_VERSION @"0.12.13"
1313

1414
#define BNC_PROD_ENV
1515
//#define BNC_STAGE_ENV

Branch-SDK/Branch-SDK/BNCDeviceInfo.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
@property (nonatomic, strong) NSNumber *screenHeight;
2929
@property (nonatomic) BOOL isAdTrackingEnabled;
3030

31+
@property (nonatomic, strong) NSString* country; // iso2 Country name (us, in,etc).
32+
@property (nonatomic, strong) NSString* language; // iso2 language code (en, ml).
33+
@property (nonatomic, strong) NSString* browserUserAgent; // Simple user agent string.
34+
3135

3236
//----------Methods----------------//
3337
+ (BNCDeviceInfo *)getInstance;
3438

35-
@end
39+
@end

Branch-SDK/Branch-SDK/BNCDeviceInfo.m

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import <Foundation/Foundation.h>
10+
#import <UIKit/UIKit.h>
1011
#import "BNCDeviceInfo.h"
1112
#import "BNCPreferenceHelper.h"
1213
#import "BNCSystemObserver.h"
@@ -27,26 +28,62 @@ + (BNCDeviceInfo *)getInstance {
2728
}
2829

2930
- (id)init {
30-
if (self = [super init]) {
31-
BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper preferenceHelper];
32-
BOOL isRealHardwareId;
33-
NSString *hardwareIdType;
34-
NSString *hardwareId = [BNCSystemObserver getUniqueHardwareId:&isRealHardwareId isDebug:preferenceHelper.isDebug andType:&hardwareIdType];
35-
if (hardwareId) {
36-
self.hardwareId = hardwareId;
37-
self.isRealHardwareId = isRealHardwareId;
38-
self.hardwareIdType = hardwareIdType;
31+
self = [super init];
32+
if (!self) return self;
33+
34+
BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper preferenceHelper];
35+
BOOL isRealHardwareId;
36+
NSString *hardwareIdType;
37+
NSString *hardwareId =
38+
[BNCSystemObserver getUniqueHardwareId:&isRealHardwareId
39+
isDebug:preferenceHelper.isDebug
40+
andType:&hardwareIdType];
41+
if (hardwareId) {
42+
self.hardwareId = hardwareId;
43+
self.isRealHardwareId = isRealHardwareId;
44+
self.hardwareIdType = hardwareIdType;
45+
}
46+
47+
self.vendorId = [BNCSystemObserver getVendorId];
48+
self.brandName = [BNCSystemObserver getBrand];
49+
self.modelName = [BNCSystemObserver getModel];
50+
self.osName = [BNCSystemObserver getOS];
51+
self.osVersion = [BNCSystemObserver getOSVersion];
52+
self.screenWidth = [BNCSystemObserver getScreenWidth];
53+
self.screenHeight = [BNCSystemObserver getScreenHeight];
54+
self.isAdTrackingEnabled = [BNCSystemObserver adTrackingSafe];
55+
56+
// Get the locale info --
57+
CGFloat systemVersion = [UIDevice currentDevice].systemVersion.floatValue;
58+
if (systemVersion < 9.0) {
59+
60+
self.language = [[NSLocale preferredLanguages] firstObject];
61+
NSString *rawLocale = [NSLocale currentLocale].localeIdentifier;
62+
NSRange range = [rawLocale rangeOfString:@"_"];
63+
if (range.location != NSNotFound) {
64+
range = NSMakeRange(range.location+1, rawLocale.length-range.location-1);
65+
self.country = [rawLocale substringWithRange:range];
3966
}
40-
41-
self.vendorId = [BNCSystemObserver getVendorId];
42-
self.brandName = [BNCSystemObserver getBrand];
43-
self.modelName = [BNCSystemObserver getModel];
44-
self.osName = [BNCSystemObserver getOS];
45-
self.osVersion = [BNCSystemObserver getOSVersion];
46-
self.screenWidth = [BNCSystemObserver getScreenWidth];
47-
self.screenHeight = [BNCSystemObserver getScreenHeight];
48-
self.isAdTrackingEnabled = [BNCSystemObserver adTrackingSafe];
67+
68+
} else if (systemVersion < 10.0) {
69+
70+
NSString *rawLanguage = [[NSLocale preferredLanguages] firstObject];
71+
NSDictionary *languageDictionary = [NSLocale componentsFromLocaleIdentifier:rawLanguage];
72+
self.country = [languageDictionary objectForKey:@"kCFLocaleCountryCodeKey"];
73+
self.language = [languageDictionary objectForKey:@"kCFLocaleLanguageCodeKey"];
74+
75+
} else {
76+
77+
self.country = [NSLocale currentLocale].countryCode;
78+
self.language = [NSLocale currentLocale].languageCode;
79+
4980
}
81+
82+
self.browserUserAgent =
83+
[[[UIWebView alloc]
84+
initWithFrame:CGRectZero]
85+
stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
86+
5087
return self;
5188
}
5289

Branch-SDK/Branch-SDK/BNCPreferenceHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
@property (assign, nonatomic) NSInteger installRequestDelay;
4040

4141
+ (BNCPreferenceHelper *)preferenceHelper;
42+
+ (NSURL*) URLForBranchDirectory;
4243

4344
- (NSString *)getAPIBaseURL;
4445
- (NSString *)getAPIURL:(NSString *)endpoint;

Branch-SDK/Branch-SDK/BNCPreferenceHelper.m

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,25 @@ - (void)writeObjectToDefaults:(NSString *)key value:(NSObject *)value {
610610
}
611611

612612
- (void)persistPrefsToDisk {
613-
NSDictionary *persistenceDict = [self.persistenceDict copy];
614-
NSBlockOperation *newPersistOp = [NSBlockOperation blockOperationWithBlock:^{
615-
if (![NSKeyedArchiver archiveRootObject:persistenceDict toFile:[self prefsFile]]) {
616-
[self logWarning:@"Failed to persist preferences to disk"];
617-
}
618-
}];
613+
@synchronized (self) {
614+
NSDictionary *persistenceDict = [self.persistenceDict copy];
615+
NSBlockOperation *newPersistOp = [NSBlockOperation blockOperationWithBlock:^ {
616+
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:persistenceDict];
617+
if (!data) {
618+
[self logWarning:@"Can't create preferences archive."];
619+
return;
620+
}
621+
NSError *error = nil;
622+
[data writeToURL:self.class.URLForPrefsFile
623+
options:NSDataWritingAtomic error:&error];
624+
if (error) {
625+
[self logWarning:
626+
[NSString stringWithFormat:
627+
@"Failed to persist preferences to disk: %@.", error]];
628+
}
629+
}];
619630
[self.persistPrefsQueue addOperation:newPersistOp];
631+
}
620632
}
621633

622634
#pragma mark - Reading From Persistence
@@ -625,20 +637,23 @@ - (NSMutableDictionary *)persistenceDict {
625637
if (!_persistenceDict) {
626638
NSDictionary *persistenceDict = nil;
627639
@try {
628-
persistenceDict = [NSKeyedUnarchiver unarchiveObjectWithFile:[self prefsFile]];
640+
NSError *error = nil;
641+
NSData *data = [NSData dataWithContentsOfURL:self.class.URLForPrefsFile
642+
options:0 error:&error];
643+
if (error || !data)
644+
NSLog(@"Error opening prefs file: %@.", error);
645+
else
646+
persistenceDict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
629647
}
630648
@catch (NSException *exception) {
631-
[self logWarning:@"Failed to load preferences from disk"];
649+
[self logWarning:@"Failed to load preferences from disk."];
632650
}
633651

634-
if (persistenceDict) {
652+
if ([persistenceDict isKindOfClass:[NSDictionary class]])
635653
_persistenceDict = [persistenceDict mutableCopy];
636-
}
637-
else {
654+
else
638655
_persistenceDict = [[NSMutableDictionary alloc] init];
639-
}
640656
}
641-
642657
return _persistenceDict;
643658
}
644659

@@ -672,8 +687,69 @@ - (NSInteger)readIntegerFromDefaults:(NSString *)key {
672687
return NSNotFound;
673688
}
674689

675-
- (NSString *)prefsFile {
676-
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:BRANCH_PREFS_FILE];
690+
+ (NSString *)prefsFile_deprecated {
691+
NSString * path =
692+
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
693+
firstObject]
694+
stringByAppendingPathComponent:BRANCH_PREFS_FILE];
695+
return path;
696+
}
697+
698+
+ (NSURL*) URLForBranchDirectory {
699+
NSError *error = nil;
700+
NSURL *URL =
701+
[[NSFileManager defaultManager]
702+
URLForDirectory:NSApplicationSupportDirectory
703+
inDomain:NSUserDomainMask
704+
appropriateForURL:nil
705+
create:YES
706+
error:&error];
707+
if (error) {
708+
NSLog(@"Error creating URLForPrefsDirectory: %@.", error);
709+
return nil;
710+
}
711+
URL = [URL URLByAppendingPathComponent:@"io.branch"];
712+
[[NSFileManager defaultManager]
713+
createDirectoryAtURL:URL
714+
withIntermediateDirectories:YES
715+
attributes:nil
716+
error:&error];
717+
if (error) {
718+
NSLog(@"Error creating URLForPrefsDirectory: %@.", error);
719+
return nil;
720+
}
721+
return URL;
722+
}
723+
724+
+ (NSURL*) URLForPrefsFile {
725+
NSURL *URL = [self URLForBranchDirectory];
726+
URL = [URL URLByAppendingPathComponent:BRANCH_PREFS_FILE];
727+
return URL;
728+
}
729+
730+
+ (void) moveOldPrefsFile {
731+
NSURL *oldURL = [NSURL fileURLWithPath:self.prefsFile_deprecated];
732+
NSURL *newURL = [self URLForPrefsFile];
733+
734+
NSError *error = nil;
735+
[[NSFileManager defaultManager]
736+
moveItemAtURL:oldURL
737+
toURL:newURL
738+
error:&error];
739+
740+
if (error && error.code != NSFileNoSuchFileError) {
741+
if (error.code == NSFileWriteFileExistsError) {
742+
[[NSFileManager defaultManager]
743+
removeItemAtURL:oldURL
744+
error:&error];
745+
} else {
746+
NSLog(@"Error moving prefs file: %@.", error);
747+
}
748+
}
749+
}
750+
751+
+ (void) initialize {
752+
[self moveOldPrefsFile];
677753
}
678754

679755
@end

Branch-SDK/Branch-SDK/BNCServerInterface.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ - (void)updateDeviceInfoToMutableDictionary:(NSMutableDictionary *)dict {
298298
[self safeSetValue:deviceInfo.osVersion forKey:BRANCH_REQUEST_KEY_OS_VERSION onDict:dict];
299299
[self safeSetValue:deviceInfo.screenWidth forKey:BRANCH_REQUEST_KEY_SCREEN_WIDTH onDict:dict];
300300
[self safeSetValue:deviceInfo.screenHeight forKey:BRANCH_REQUEST_KEY_SCREEN_HEIGHT onDict:dict];
301-
301+
302+
[self safeSetValue:deviceInfo.browserUserAgent forKey:@"user_agent" onDict:dict];
303+
[self safeSetValue:deviceInfo.country forKey:@"country" onDict:dict];
304+
[self safeSetValue:deviceInfo.language forKey:@"language" onDict:dict];
305+
302306
dict[BRANCH_REQUEST_KEY_AD_TRACKING_ENABLED] = @(deviceInfo.isAdTrackingEnabled);
303307

304308
}

0 commit comments

Comments
 (0)