|
|
@ -62,21 +62,28 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
|
|
|
|
+ (PhoneNumber *)phoneNumberFromUserSpecifiedText:(NSString *)text {
|
|
|
|
+ (PhoneNumber *)phoneNumberFromUserSpecifiedText:(NSString *)text {
|
|
|
|
OWSAssert(text != nil);
|
|
|
|
OWSAssert(text != nil);
|
|
|
|
|
|
|
|
|
|
|
|
return [PhoneNumber phoneNumberFromText:text andRegion:[self defaultRegionCode]];
|
|
|
|
return [PhoneNumber phoneNumberFromText:text andRegion:[self defaultCountryCode]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (NSString *)defaultRegionCode {
|
|
|
|
+ (NSString *)defaultCountryCode
|
|
|
|
NSString *defaultRegion;
|
|
|
|
{
|
|
|
|
|
|
|
|
NSLocale *locale = [NSLocale currentLocale];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NSString *_Nullable countryCode = nil;
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
defaultRegion = [[PhoneNumberUtil sharedUtil].nbPhoneNumberUtil countryCodeByCarrier];
|
|
|
|
countryCode = [[PhoneNumberUtil sharedUtil].nbPhoneNumberUtil countryCodeByCarrier];
|
|
|
|
|
|
|
|
|
|
|
|
if ([defaultRegion isEqualToString:@"ZZ"]) {
|
|
|
|
if ([countryCode isEqualToString:@"ZZ"]) {
|
|
|
|
defaultRegion = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
|
|
|
|
countryCode = [locale objectForKey:NSLocaleCountryCode];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
defaultRegion = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
|
|
|
|
countryCode = [locale objectForKey:NSLocaleCountryCode];
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return defaultRegion;
|
|
|
|
if (!countryCode) {
|
|
|
|
|
|
|
|
OWSFail(@"%@ Could not identify country code for locale: %@", self.logTag, locale);
|
|
|
|
|
|
|
|
countryCode = @"US";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return countryCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (PhoneNumber *)phoneNumberFromE164:(NSString *)text {
|
|
|
|
+ (PhoneNumber *)phoneNumberFromE164:(NSString *)text {
|
|
|
@ -90,7 +97,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
|
|
|
|
|
|
|
|
|
|
|
|
+ (NSString *)bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:(NSString *)input {
|
|
|
|
+ (NSString *)bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:(NSString *)input {
|
|
|
|
return [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:input
|
|
|
|
return [PhoneNumber bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:input
|
|
|
|
withSpecifiedRegionCode:[self defaultRegionCode]];
|
|
|
|
withSpecifiedRegionCode:[self defaultCountryCode]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (NSString *)bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:(NSString *)input
|
|
|
|
+ (NSString *)bestEffortFormatPartialUserSpecifiedTextToLookLikeAPhoneNumber:(NSString *)input
|
|
|
@ -144,9 +151,9 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
|
|
|
|
static NSString *result = nil;
|
|
|
|
static NSString *result = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
NSString *defaultRegionCode = [self defaultRegionCode];
|
|
|
|
NSString *defaultCountryCode = [self defaultCountryCode];
|
|
|
|
NBMetadataHelper *helper = [[NBMetadataHelper alloc] init];
|
|
|
|
NBMetadataHelper *helper = [[NBMetadataHelper alloc] init];
|
|
|
|
NBPhoneMetaData *defaultRegionMetadata = [helper getMetadataForRegion:defaultRegionCode];
|
|
|
|
NBPhoneMetaData *defaultRegionMetadata = [helper getMetadataForRegion:defaultCountryCode];
|
|
|
|
result = defaultRegionMetadata.nationalPrefixTransformRule;
|
|
|
|
result = defaultRegionMetadata.nationalPrefixTransformRule;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
@ -168,7 +175,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
|
|
|
|
NSString *localCallingCodePrefix = [NSString stringWithFormat:@"+%@", localCallingCode];
|
|
|
|
NSString *localCallingCodePrefix = [NSString stringWithFormat:@"+%@", localCallingCode];
|
|
|
|
NSString *localCountryCode =
|
|
|
|
NSString *localCountryCode =
|
|
|
|
[PhoneNumberUtil.sharedUtil probableCountryCodeForCallingCode:localCallingCodePrefix];
|
|
|
|
[PhoneNumberUtil.sharedUtil probableCountryCodeForCallingCode:localCallingCodePrefix];
|
|
|
|
if (localCountryCode && ![localCountryCode isEqualToString:[self defaultRegionCode]]) {
|
|
|
|
if (localCountryCode && ![localCountryCode isEqualToString:[self defaultCountryCode]]) {
|
|
|
|
NBMetadataHelper *helper = [[NBMetadataHelper alloc] init];
|
|
|
|
NBMetadataHelper *helper = [[NBMetadataHelper alloc] init];
|
|
|
|
NBPhoneMetaData *localNumberRegionMetadata = [helper getMetadataForRegion:localCountryCode];
|
|
|
|
NBPhoneMetaData *localNumberRegionMetadata = [helper getMetadataForRegion:localCountryCode];
|
|
|
|
result = localNumberRegionMetadata.nationalPrefixTransformRule;
|
|
|
|
result = localNumberRegionMetadata.nationalPrefixTransformRule;
|
|
|
@ -248,7 +255,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
tryParsingWithCountryCode(sanitizedString, [self defaultRegionCode]);
|
|
|
|
tryParsingWithCountryCode(sanitizedString, [self defaultCountryCode]);
|
|
|
|
|
|
|
|
|
|
|
|
if ([sanitizedString hasPrefix:@"+"]) {
|
|
|
|
if ([sanitizedString hasPrefix:@"+"]) {
|
|
|
|
// If the text starts with "+", don't try prepending
|
|
|
|
// If the text starts with "+", don't try prepending
|
|
|
@ -257,7 +264,7 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Try just adding "+" and parsing it.
|
|
|
|
// 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
|
|
|
|
// Order matters; better results should appear first so prefer
|
|
|
|
// matches with the same country code as this client's phone number.
|
|
|
|
// 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];
|
|
|
|
NSString *callingCodePrefix = [NSString stringWithFormat:@"+%@", callingCodeForLocalNumber];
|
|
|
|
|
|
|
|
|
|
|
|
tryParsingWithCountryCode(
|
|
|
|
tryParsingWithCountryCode(
|
|
|
|
[callingCodePrefix stringByAppendingString:sanitizedString], [self defaultRegionCode]);
|
|
|
|
[callingCodePrefix stringByAppendingString:sanitizedString], [self defaultCountryCode]);
|
|
|
|
|
|
|
|
|
|
|
|
// Try to determine what the country code is for the local phone number
|
|
|
|
// 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
|
|
|
|
// 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.
|
|
|
|
// phone. They're likely to have both Italian and French contacts.
|
|
|
|
NSString *localCountryCode =
|
|
|
|
NSString *localCountryCode =
|
|
|
|
[PhoneNumberUtil.sharedUtil probableCountryCodeForCallingCode:callingCodePrefix];
|
|
|
|
[PhoneNumberUtil.sharedUtil probableCountryCodeForCallingCode:callingCodePrefix];
|
|
|
|
if (localCountryCode && ![localCountryCode isEqualToString:[self defaultRegionCode]]) {
|
|
|
|
if (localCountryCode && ![localCountryCode isEqualToString:[self defaultCountryCode]]) {
|
|
|
|
tryParsingWithCountryCode(
|
|
|
|
tryParsingWithCountryCode(
|
|
|
|
[callingCodePrefix stringByAppendingString:sanitizedString], localCountryCode);
|
|
|
|
[callingCodePrefix stringByAppendingString:sanitizedString], localCountryCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|