diff --git a/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m b/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m index e53e07612..11897ce2f 100644 --- a/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m +++ b/Signal/src/ViewControllers/AdvancedSettingsTableViewController.m @@ -215,7 +215,7 @@ NS_ASSUME_NONNULL_BEGIN } if (!countryMetadata) { - countryCode = [NSLocale.currentLocale objectForKey:NSLocaleCountryCode]; + countryCode = [PhoneNumber defaultCountryCode]; if (countryCode) { countryMetadata = [OWSCountryMetadata countryMetadataForCountryCode:countryCode]; } diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m index 0bf684838..8b7c8b465 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m @@ -98,7 +98,7 @@ NS_ASSUME_NONNULL_BEGIN } if (!countryMetadata) { - countryCode = [NSLocale.currentLocale objectForKey:NSLocaleCountryCode]; + countryCode = [PhoneNumber defaultCountryCode]; if (countryCode) { countryMetadata = [OWSCountryMetadata countryMetadataForCountryCode:countryCode]; } diff --git a/Signal/src/ViewControllers/RegistrationViewController.m b/Signal/src/ViewControllers/RegistrationViewController.m index 134cfab14..3ac304cfd 100644 --- a/Signal/src/ViewControllers/RegistrationViewController.m +++ b/Signal/src/ViewControllers/RegistrationViewController.m @@ -241,8 +241,7 @@ NSString *const kKeychainKey_LastRegisteredPhoneNumber = @"kKeychainKey_LastRegi #pragma mark - Country - (void)populateDefaultCountryNameAndCode { - NSLocale *locale = NSLocale.currentLocale; - NSString *countryCode = [locale objectForKey:NSLocaleCountryCode]; + NSString *countryCode = [PhoneNumber defaultCountryCode]; #ifdef DEBUG if ([self lastRegisteredCountryCode].length > 0) { diff --git a/Signal/src/ViewControllers/SelectRecipientViewController.m b/Signal/src/ViewControllers/SelectRecipientViewController.m index 9c7671a80..2be9c7796 100644 --- a/Signal/src/ViewControllers/SelectRecipientViewController.m +++ b/Signal/src/ViewControllers/SelectRecipientViewController.m @@ -224,8 +224,7 @@ NSString *const kSelectRecipientViewControllerCellIdentifier = @"kSelectRecipien } if (!countryCode || !callingCode) { - NSLocale *locale = NSLocale.currentLocale; - countryCode = [locale objectForKey:NSLocaleCountryCode]; + countryCode = [PhoneNumber defaultCountryCode]; callingCode = [[PhoneNumberUtil sharedUtil].nbPhoneNumberUtil getCountryCodeForRegion:countryCode]; } diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index bdf1f82a1..5e9397707 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -1573,6 +1573,9 @@ /* Displayed if for some reason we can't determine a contacts phone number *or* name */ "UNKNOWN_CONTACT_NAME" = "Unknown Contact"; +/* Indicates an unknown or unrecognizable value. */ +"UNKNOWN_VALUE" = "Unknown"; + /* button title for unlinking a device */ "UNLINK_ACTION" = "Unlink"; diff --git a/SignalServiceKit/src/Contacts/PhoneNumber.h b/SignalServiceKit/src/Contacts/PhoneNumber.h index f5161e662..2d717bc9c 100644 --- a/SignalServiceKit/src/Contacts/PhoneNumber.h +++ b/SignalServiceKit/src/Contacts/PhoneNumber.h @@ -42,4 +42,6 @@ - (NSComparisonResult)compare:(PhoneNumber *)other; ++ (NSString *)defaultCountryCode; + @end diff --git a/SignalServiceKit/src/Contacts/PhoneNumber.m b/SignalServiceKit/src/Contacts/PhoneNumber.m index 82c808a8a..b26b6848a 100644 --- a/SignalServiceKit/src/Contacts/PhoneNumber.m +++ b/SignalServiceKit/src/Contacts/PhoneNumber.m @@ -62,21 +62,28 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN + (PhoneNumber *)phoneNumberFromUserSpecifiedText:(NSString *)text { OWSAssert(text != nil); - return [PhoneNumber phoneNumberFromText:text andRegion:[self defaultRegionCode]]; + return [PhoneNumber phoneNumberFromText:text andRegion:[self defaultCountryCode]]; } -+ (NSString *)defaultRegionCode { - NSString *defaultRegion; ++ (NSString *)defaultCountryCode +{ + NSLocale *locale = [NSLocale currentLocale]; + + NSString *_Nullable countryCode = nil; #if TARGET_OS_IPHONE - defaultRegion = [[PhoneNumberUtil sharedUtil].nbPhoneNumberUtil countryCodeByCarrier]; + countryCode = [[PhoneNumberUtil sharedUtil].nbPhoneNumberUtil countryCodeByCarrier]; - if ([defaultRegion isEqualToString:@"ZZ"]) { - defaultRegion = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]; + if ([countryCode isEqualToString:@"ZZ"]) { + countryCode = [locale objectForKey:NSLocaleCountryCode]; } #else - defaultRegion = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]; + countryCode = [locale objectForKey:NSLocaleCountryCode]; #endif - return defaultRegion; + if (!countryCode) { + OWSFail(@"%@ Could not identify country code for locale: %@", self.logTag, locale); + countryCode = @"US"; + } + return countryCode; } + (PhoneNumber *)phoneNumberFromE164:(NSString *)text { @@ -90,7 +97,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN + (NSString *)bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:(NSString *)input { return [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:input - withSpecifiedRegionCode:[self defaultRegionCode]]; + withSpecifiedRegionCode:[self defaultCountryCode]]; } + (NSString *)bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:(NSString *)input @@ -144,9 +151,9 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN static NSString *result = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - NSString *defaultRegionCode = [self defaultRegionCode]; + NSString *defaultCountryCode = [self defaultCountryCode]; NBMetadataHelper *helper = [[NBMetadataHelper alloc] init]; - NBPhoneMetaData *defaultRegionMetadata = [helper getMetadataForRegion:defaultRegionCode]; + NBPhoneMetaData *defaultRegionMetadata = [helper getMetadataForRegion:defaultCountryCode]; result = defaultRegionMetadata.nationalPrefixTransformRule; }); return result; @@ -168,7 +175,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN NSString *localCallingCodePrefix = [NSString stringWithFormat:@"+%@", localCallingCode]; NSString *localCountryCode = [PhoneNumberUtil.sharedUtil probableCountryCodeForCallingCode:localCallingCodePrefix]; - if (localCountryCode && ![localCountryCode isEqualToString:[self defaultRegionCode]]) { + if (localCountryCode && ![localCountryCode isEqualToString:[self defaultCountryCode]]) { NBMetadataHelper *helper = [[NBMetadataHelper alloc] init]; NBPhoneMetaData *localNumberRegionMetadata = [helper getMetadataForRegion:localCountryCode]; result = localNumberRegionMetadata.nationalPrefixTransformRule; @@ -248,7 +255,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN } }; - tryParsingWithCountryCode(sanitizedString, [self defaultRegionCode]); + tryParsingWithCountryCode(sanitizedString, [self defaultCountryCode]); if ([sanitizedString hasPrefix:@"+"]) { // If the text starts with "+", don't try prepending @@ -257,7 +264,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN } // Try just adding "+" and parsing it. - tryParsingWithCountryCode([NSString stringWithFormat:@"+%@", sanitizedString], [self defaultRegionCode]); + tryParsingWithCountryCode([NSString stringWithFormat:@"+%@", sanitizedString], [self defaultCountryCode]); // Order matters; better results should appear first so prefer // matches with the same country code as this client's phone number. @@ -272,7 +279,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN NSString *callingCodePrefix = [NSString stringWithFormat:@"+%@", callingCodeForLocalNumber]; tryParsingWithCountryCode( - [callingCodePrefix stringByAppendingString:sanitizedString], [self defaultRegionCode]); + [callingCodePrefix stringByAppendingString:sanitizedString], [self defaultCountryCode]); // Try to determine what the country code is for the local phone number // and also try parsing the phone number using that country code if it @@ -283,7 +290,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN // phone. They're likely to have both Italian and French contacts. NSString *localCountryCode = [PhoneNumberUtil.sharedUtil probableCountryCodeForCallingCode:callingCodePrefix]; - if (localCountryCode && ![localCountryCode isEqualToString:[self defaultRegionCode]]) { + if (localCountryCode && ![localCountryCode isEqualToString:[self defaultCountryCode]]) { tryParsingWithCountryCode( [callingCodePrefix stringByAppendingString:sanitizedString], localCountryCode); } diff --git a/SignalServiceKit/src/Contacts/PhoneNumberUtil.m b/SignalServiceKit/src/Contacts/PhoneNumberUtil.m index 8fe4d5c9c..9b0992196 100644 --- a/SignalServiceKit/src/Contacts/PhoneNumberUtil.m +++ b/SignalServiceKit/src/Contacts/PhoneNumberUtil.m @@ -82,13 +82,18 @@ // country code -> country name + (NSString *)countryNameFromCountryCode:(NSString *)countryCode { + OWSAssert(countryCode); + NSDictionary *countryCodeComponent = @{NSLocaleCountryCode : countryCode}; NSString *identifier = [NSLocale localeIdentifierFromComponents:countryCodeComponent]; - NSString *country = [NSLocale.currentLocale displayNameForKey:NSLocaleIdentifier value:identifier]; - if (country.length < 1) { - country = [NSLocale.systemLocale displayNameForKey:NSLocaleIdentifier value:identifier]; + NSString *countryName = [NSLocale.currentLocale displayNameForKey:NSLocaleIdentifier value:identifier]; + if (countryName.length < 1) { + countryName = [NSLocale.systemLocale displayNameForKey:NSLocaleIdentifier value:identifier]; + } + if (countryName.length < 1) { + countryName = NSLocalizedString(@"UNKNOWN_VALUE", "Indicates an unknown or unrecognizable value."); } - return country; + return countryName; } // country code -> calling code