From fcc7eb656565333956a78814b4c1202425983933 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 19 May 2017 22:30:49 -0400 Subject: [PATCH] Try the country code for the local phone number when parsing phone numbers. // FREEBIE --- src/Contacts/PhoneNumber.m | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Contacts/PhoneNumber.m b/src/Contacts/PhoneNumber.m index bf1df44e0..39e1a68ff 100644 --- a/src/Contacts/PhoneNumber.m +++ b/src/Contacts/PhoneNumber.m @@ -165,10 +165,24 @@ static NSString *const RPDefaultsKeyPhoneNumberCanonical = @"RPDefaultsKeyPhoneN // (ISO 3166-1 alpha-2). NSNumber *callingCodeForLocalNumber = [[PhoneNumber phoneNumberFromE164:clientPhoneNumber] getCountryCode]; if (callingCodeForLocalNumber != nil) { - tryParsingWithCountryCode([NSString stringWithFormat:@"+%@%@", - callingCodeForLocalNumber, - sanitizedString], - [self defaultRegionCode]); + NSString *callingCodePrefix = [NSString stringWithFormat:@"+%@", callingCodeForLocalNumber]; + + tryParsingWithCountryCode( + [callingCodePrefix stringByAppendingString:sanitizedString], [self defaultRegionCode]); + + // 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 + // differs from the device's region code. + // + // For example, a French person living in Italy might have an + // Italian phone number but use French region/language for their + // phone. They're likely to have both Italian and French contacts. + NSString *localCountryCode = + [PhoneNumberUtil.sharedUtil probableCountryCodeForCallingCode:callingCodePrefix]; + if (localCountryCode && ![localCountryCode isEqualToString:[self defaultRegionCode]]) { + tryParsingWithCountryCode( + [callingCodePrefix stringByAppendingString:sanitizedString], localCountryCode); + } } }