Skip to content

Commit cf61fd1

Browse files
authored
Merge pull request #1159 from firebase/mc/ct
Add API for manually specifying default phone number country code
2 parents 8a120fb + 053c311 commit cf61fd1

File tree

7 files changed

+53
-54
lines changed

7 files changed

+53
-54
lines changed

FirebasePhoneAuthUI/Sources/FUICountryCodes.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
NS_ASSUME_NONNULL_BEGIN
2020

21-
@interface FUICountryCodeInfo : NSObject
21+
@interface FUICountryCodeInfo : NSObject <NSCopying>
2222

2323
@property (nonatomic, copy) NSString *countryName;
2424
@property (nonatomic, copy) NSString *localizedCountryName;
@@ -32,6 +32,13 @@ NS_ASSUME_NONNULL_BEGIN
3232

3333
@interface FUICountryCodes : NSObject
3434

35+
/** @property defaultCountryCodeInfo
36+
@brief Get the default country code info
37+
The default country code. Formerly this was set via CTCarrier information, but CTCarrier is now
38+
deprecated, so this value must be manually set. Defaults to the country code for the United States.
39+
*/
40+
@property (nonatomic, copy) FUICountryCodeInfo *defaultCountryCodeInfo;
41+
3542
/** @fn count:
3643
@brief Return the number of available country codes.
3744
*/
@@ -55,16 +62,6 @@ NS_ASSUME_NONNULL_BEGIN
5562
*/
5663
- (nullable FUICountryCodeInfo *)countryCodeInfoForCode:(NSString *)countryCode;
5764

58-
/** @fn defaultCountryCodeInfo
59-
@brief Get the default country code info
60-
@detail The default country is retrieved based on the following logic:
61-
1. The country code info of user's carrier provider if available.
62-
2. The country code info of user's device locale, if available.
63-
3. A hard coded coutry code info (US), if available.
64-
4. The first available country code info in the instance.
65-
*/
66-
- (FUICountryCodeInfo *)defaultCountryCodeInfo;
67-
6865
/** @fn blacklistCountries:
6966
@brief Remove the set of countries from available country codes.
7067
@param countries A set of blacklisted country codes. Country codes are in NSString format, and

FirebasePhoneAuthUI/Sources/FUICountryCodes.m

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ - (NSString *)countryFlagEmoji {
5757
length:sizeof(bytes)
5858
encoding:NSUTF32LittleEndianStringEncoding];
5959
}
60+
61+
- (id)copyWithZone:(NSZone *_Nullable)zone {
62+
FUICountryCodeInfo *newInfo = [[FUICountryCodeInfo alloc] init];
63+
newInfo.countryName = self.countryName;
64+
newInfo.localizedCountryName = self.localizedCountryName;
65+
newInfo.countryCode = self.countryCode;
66+
newInfo.dialCode = self.dialCode;
67+
newInfo.level = self.level;
68+
return newInfo;
69+
}
70+
71+
- (BOOL)isEqual:(FUICountryCodeInfo *)object {
72+
if (object == self) {
73+
return YES;
74+
}
75+
if (![object isKindOfClass:[self class]]) {
76+
return NO;
77+
}
78+
return object.countryName == self.countryName &&
79+
object.localizedCountryName == self.localizedCountryName &&
80+
object.countryCode == self.countryCode &&
81+
object.dialCode == self.dialCode &&
82+
object.level == self.level;
83+
}
84+
6085
@end
6186

6287
@interface FUICountryCodes ()
@@ -114,48 +139,10 @@ - (FUICountryCodeInfo *)countryCodeInfoAtIndex:(NSInteger)index {
114139
}
115140

116141
- (FUICountryCodeInfo *)defaultCountryCodeInfo {
117-
// Get the country code based on the information of user's telecommunication carrier provider.
118-
CTCarrier *carrier;
119-
if (@available(iOS 12, *)) {
120-
NSDictionary *carriers =
121-
[[[CTTelephonyNetworkInfo alloc] init] serviceSubscriberCellularProviders];
122-
// For multi-sim phones, use the current locale to make an educated guess for
123-
// which carrier to use.
124-
NSString *currentCountryCode = [NSLocale currentLocale].countryCode;
125-
for (CTCarrier *provider in carriers.allValues) {
126-
if ([provider isKindOfClass:[CTCarrier class]] &&
127-
[provider.isoCountryCode isEqualToString:currentCountryCode]) {
128-
carrier = provider;
129-
break;
130-
}
131-
}
132-
133-
// If the carrier is still nil, grab a random carrier from the dictionary.
134-
if (carrier == nil) {
135-
for (CTCarrier *provider in carriers.allValues) {
136-
if ([provider isKindOfClass:[CTCarrier class]]) {
137-
carrier = provider;
138-
break;
139-
}
140-
}
141-
}
142-
} else {
143-
#pragma clang diagnostic push
144-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
145-
carrier = [[[CTTelephonyNetworkInfo alloc] init] subscriberCellularProvider];
146-
#pragma clang diagnostic pop
142+
if (_defaultCountryCodeInfo == nil) {
143+
return [self countryCodeInfoForCode:kFUIDefaultCountryCode] ?: [self countryCodeInfoAtIndex:0];
147144
}
148-
NSString *countryCode = carrier.isoCountryCode ?: [[self class] countryCodeFromDeviceLocale];
149-
FUICountryCodeInfo *countryCodeInfo = [self countryCodeInfoForCode:countryCode];
150-
// If carrier is not available, get the hard coded default country code.
151-
if (!countryCodeInfo) {
152-
countryCodeInfo = [self countryCodeInfoForCode:kFUIDefaultCountryCode];
153-
}
154-
// If the hard coded default country code is not available, get the first available country code.
155-
if (!countryCodeInfo) {
156-
countryCodeInfo = [self countryCodeInfoAtIndex:0];
157-
}
158-
return countryCodeInfo;
145+
return _defaultCountryCodeInfo;
159146
}
160147

161148
- (FUICountryCodeInfo *)countryCodeInfoForPhoneNumber:(NSString *)phoneNumber {

FirebasePhoneAuthUI/Sources/FUIPhoneEntryViewController.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ - (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil
113113
if (self) {
114114
self.title = FUIPhoneAuthLocalizedString(kPAStr_EnterPhoneTitle);
115115
_countryCodes = countryCodes ?: [[FUICountryCodes alloc] init];
116+
FUIPhoneAuth *provider = [authUI providerWithID:FIRPhoneAuthProviderID];
117+
NSString *defaultCountryCode = provider.defaultCountryCode;
118+
_countryCodes.defaultCountryCodeInfo =
119+
[_countryCodes countryCodeInfoForCode:defaultCountryCode];
116120
if (phoneNumber.length) {
117121
_phoneNumber = [[FUIPhoneNumber alloc] initWithNormalizedPhoneNumber:phoneNumber
118122
countryCodes:_countryCodes];

FirebasePhoneAuthUI/Sources/FUIPhoneNumber.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ - (instancetype)initWithNormalizedPhoneNumber:(NSString *)normalizedPhoneNumber
4040
}
4141
if (!rawPhoneNumber) {
4242
rawPhoneNumber = normalizedPhoneNumber;
43-
countryCode = [countryCodes defaultCountryCodeInfo];
43+
countryCode = countryCodes.defaultCountryCodeInfo;
4444
}
4545
return [self initWithRawPhoneNumber:rawPhoneNumber countryCode:countryCode];
4646
}

FirebasePhoneAuthUI/Sources/Public/FirebasePhoneAuthUI/FUIPhoneAuth.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ NS_ASSUME_NONNULL_BEGIN
2828
*/
2929
@property(nonatomic, readwrite) FUIButtonAlignment buttonAlignment;
3030

31+
/** @property defaultCountryCode
32+
Returns the default country code. If unspecified, this will default to the United States country code.
33+
If overwriting the default country code, this value must be set before the phone auth flow is
34+
presented.
35+
*/
36+
@property (nonatomic, copy, readwrite) NSString *defaultCountryCode;
37+
3138
/** @fn bundle
3239
@brief Returns the resource bundle required by this class.
3340
*/

samples/swift/FirebaseUI-demo-swift/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>FacebookClientToken</key>
6+
<string>aaaaa</string>
57
<key>CFBundleDevelopmentRegion</key>
68
<string>en</string>
79
<key>CFBundleExecutable</key>

samples/swift/FirebaseUI-demo-swift/Samples/Auth/FUIAuthViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ class FUIAuthViewController: UITableViewController {
323323
provider = nil
324324
}
325325
case Providers.Phone.rawValue:
326-
provider = FUIPhoneAuth(authUI: self.authUI!)
326+
let phoneAuth = FUIPhoneAuth(authUI: self.authUI!)
327+
phoneAuth.defaultCountryCode = "JP"
328+
provider = phoneAuth
327329
default: provider = nil
328330
}
329331

0 commit comments

Comments
 (0)