Skip to content

Commit e0d2bce

Browse files
authored
Decoupled country code discovery from language discovery. (#650)
1 parent 7a6b1c0 commit e0d2bce

File tree

1 file changed

+68
-23
lines changed

1 file changed

+68
-23
lines changed

Branch-SDK/Branch-SDK/BNCDeviceInfo.m

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,80 @@ - (id)init {
5858
self.screenHeight = [BNCSystemObserver getScreenHeight];
5959
self.isAdTrackingEnabled = [BNCSystemObserver adTrackingSafe];
6060

61+
self.country = [self.class bnc_country];
62+
self.language = [self.class bnc_language];
63+
self.browserUserAgent = [self.class userAgentString];
64+
return self;
65+
}
66+
67+
+ (NSString*) bnc_country {
68+
69+
NSString *country = nil;
70+
#define returnIfValidCountry() \
71+
if ([country isKindOfClass:[NSString class]] && country.length) { \
72+
return country; \
73+
} else { \
74+
country = nil; \
75+
}
76+
77+
// Should work on iOS 10
6178
NSLocale *currentLocale = [NSLocale currentLocale];
62-
if ([currentLocale respondsToSelector:@selector(countryCode)] &&
63-
[currentLocale respondsToSelector:@selector(languageCode)]) {
64-
// Should work on iOS 10
65-
self.country = [currentLocale countryCode];
66-
self.language = [currentLocale languageCode];
79+
if ([currentLocale respondsToSelector:@selector(countryCode)]) {
80+
country = [currentLocale countryCode];
6781
}
68-
69-
if (!self.country) {
70-
// Should work on iOS 9
71-
NSString *rawLanguage = [[NSLocale preferredLanguages] firstObject];
72-
NSDictionary *languageDictionary = [NSLocale componentsFromLocaleIdentifier:rawLanguage];
73-
self.country = [languageDictionary objectForKey:@"kCFLocaleCountryCodeKey"];
74-
self.language = [languageDictionary objectForKey:@"kCFLocaleLanguageCodeKey"];
82+
returnIfValidCountry();
83+
84+
// Should work on iOS 9
85+
NSString *rawLanguage = [[NSLocale preferredLanguages] firstObject];
86+
NSDictionary *languageDictionary = [NSLocale componentsFromLocaleIdentifier:rawLanguage];
87+
country = [languageDictionary objectForKey:@"kCFLocaleCountryCodeKey"];
88+
returnIfValidCountry();
89+
90+
// Should work on iOS 8 and below.
91+
//NSString* language = [[NSLocale preferredLanguages] firstObject];
92+
NSString *rawLocale = currentLocale.localeIdentifier;
93+
NSRange range = [rawLocale rangeOfString:@"_"];
94+
if (range.location != NSNotFound) {
95+
range = NSMakeRange(range.location+1, rawLocale.length-range.location-1);
96+
country = [rawLocale substringWithRange:range];
7597
}
98+
returnIfValidCountry();
7699

77-
if (!self.country) {
78-
// Should work on iOS 8 and below.
79-
self.language = [[NSLocale preferredLanguages] firstObject];
80-
NSString *rawLocale = currentLocale.localeIdentifier;
81-
NSRange range = [rawLocale rangeOfString:@"_"];
82-
if (range.location != NSNotFound) {
83-
range = NSMakeRange(range.location+1, rawLocale.length-range.location-1);
84-
self.country = [rawLocale substringWithRange:range];
85-
}
100+
#undef returnIfValidCountry
101+
102+
return nil;
103+
}
104+
105+
+ (NSString*) bnc_language {
106+
107+
NSString *language = nil;
108+
#define returnIfValidLanguage() \
109+
if ([language isKindOfClass:[NSString class]] && language.length) { \
110+
return language; \
111+
} else { \
112+
language = nil; \
113+
} \
114+
115+
// Should work on iOS 10
116+
NSLocale *currentLocale = [NSLocale currentLocale];
117+
if ([currentLocale respondsToSelector:@selector(languageCode)]) {
118+
language = [currentLocale languageCode];
86119
}
120+
returnIfValidLanguage();
87121

88-
self.browserUserAgent = [self.class userAgentString];
89-
return self;
122+
// Should work on iOS 9
123+
NSString *rawLanguage = [[NSLocale preferredLanguages] firstObject];
124+
NSDictionary *languageDictionary = [NSLocale componentsFromLocaleIdentifier:rawLanguage];
125+
language = [languageDictionary objectForKey:@"kCFLocaleLanguageCodeKey"];
126+
returnIfValidLanguage();
127+
128+
// Should work on iOS 8 and below.
129+
language = [[NSLocale preferredLanguages] firstObject];
130+
returnIfValidLanguage();
131+
132+
#undef returnIfValidLanguage
133+
134+
return nil;
90135
}
91136

92137
+ (NSString*) systemBuildVersion {

0 commit comments

Comments
 (0)