|  | 
| 16 | 16 | #import "NBPhoneNumberDesc.h" | 
| 17 | 17 | #import "NBRegExMatcher.h" | 
| 18 | 18 | 
 | 
| 19 |  | -#if TARGET_OS_IOS | 
| 20 |  | -#import <CoreTelephony/CTCarrier.h> | 
| 21 |  | -#import <CoreTelephony/CTTelephonyNetworkInfo.h> | 
|  | 19 | +#if __has_include(<Contacts/Contacts.h>) | 
|  | 20 | +#import "Contacts/Contacts.h" | 
| 22 | 21 | #endif | 
| 23 | 22 | 
 | 
| 24 | 23 | static NSString *NormalizeNonBreakingSpace(NSString *aString) { | 
| @@ -52,10 +51,6 @@ @interface NBPhoneNumberUtil () | 
| 52 | 51 | @property(nonatomic, strong, readwrite) NBMetadataHelper *helper; | 
| 53 | 52 | @property(nonatomic, strong, readwrite) NBRegExMatcher *matcher; | 
| 54 | 53 | 
 | 
| 55 |  | -#if TARGET_OS_IOS | 
| 56 |  | -@property(nonatomic, readonly) CTTelephonyNetworkInfo *telephonyNetworkInfo; | 
| 57 |  | -#endif | 
| 58 |  | - | 
| 59 | 54 | @end | 
| 60 | 55 | 
 | 
| 61 | 56 | @implementation NBPhoneNumberUtil | 
| @@ -3452,62 +3447,26 @@ - (NBPhoneNumber *)parse:(NSString *)numberToParse | 
| 3452 | 3447 | } | 
| 3453 | 3448 | 
 | 
| 3454 | 3449 | /** | 
| 3455 |  | - * Parses a string using the phone's carrier region (when available, ZZ otherwise). | 
| 3456 |  | - * This uses the country the sim card in the phone is registered with. | 
|  | 3450 | + * Parses a string using the phone's carrier region (when available, uses system locale otherwise). | 
|  | 3451 | + * This uses the country the SIM card in the phone is registered with. | 
| 3457 | 3452 |  * For example if you have an AT&T sim card but are in Europe, this will parse the | 
| 3458 | 3453 |  * number using +1 (AT&T is a US Carrier) as the default country code. | 
| 3459 |  | - * This also works for CDMA phones which don't have a sim card. | 
|  | 3454 | + * This also works for multi-SIM phones, using the SIM region of default voice line. | 
| 3460 | 3455 |  */ | 
| 3461 | 3456 | - (NBPhoneNumber *)parseWithPhoneCarrierRegion:(NSString *)numberToParse error:(NSError **)error { | 
| 3462 | 3457 |   numberToParse = NormalizeNonBreakingSpace(numberToParse); | 
| 3463 | 3458 | 
 | 
| 3464 |  | -  NSString *defaultRegion = nil; | 
| 3465 |  | -#if TARGET_OS_IOS | 
| 3466 |  | -  defaultRegion = [self countryCodeByCarrier]; | 
| 3467 |  | -#else | 
| 3468 |  | -  defaultRegion = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]; | 
| 3469 |  | -#endif | 
| 3470 |  | -  if ([NB_UNKNOWN_REGION isEqualToString:defaultRegion]) { | 
| 3471 |  | -    // get region from device as a failover (e.g. iPad) | 
| 3472 |  | -    NSLocale *currentLocale = [NSLocale currentLocale]; | 
| 3473 |  | -    defaultRegion = [currentLocale objectForKey:NSLocaleCountryCode]; | 
| 3474 |  | -  } | 
| 3475 |  | - | 
|  | 3459 | +  NSString *defaultRegion = [self countryCodeByCarrier]; | 
| 3476 | 3460 |   return [self parse:numberToParse defaultRegion:defaultRegion error:error]; | 
| 3477 | 3461 | } | 
| 3478 | 3462 | 
 | 
| 3479 |  | -#if TARGET_OS_IOS | 
| 3480 |  | - | 
| 3481 |  | -static CTTelephonyNetworkInfo *_telephonyNetworkInfo; | 
| 3482 |  | - | 
| 3483 |  | -- (CTTelephonyNetworkInfo *)telephonyNetworkInfo { | 
| 3484 |  | -  // cache telephony network info; | 
| 3485 |  | -  // CTTelephonyNetworkInfo objects are unnecessarily created for every call to | 
| 3486 |  | -  // parseWithPhoneCarrierRegion:error: when in reality this information not change while an app | 
| 3487 |  | -  // lives in memory real-world performance test while parsing 93 phone numbers: before change: | 
| 3488 |  | -  // 126ms after change:    32ms using static instance prevents deallocation crashes due to ios bug | 
| 3489 |  | - | 
| 3490 |  | -  static dispatch_once_t onceToken; | 
| 3491 |  | -  dispatch_once(&onceToken, ^{ | 
| 3492 |  | -    _telephonyNetworkInfo = [[CTTelephonyNetworkInfo alloc] init]; | 
| 3493 |  | -  }); | 
| 3494 |  | - | 
| 3495 |  | -  return _telephonyNetworkInfo; | 
| 3496 |  | -} | 
| 3497 |  | - | 
| 3498 | 3463 | - (NSString *)countryCodeByCarrier { | 
| 3499 |  | -  NSString *isoCode = [[self.telephonyNetworkInfo subscriberCellularProvider] isoCountryCode]; | 
| 3500 |  | - | 
| 3501 |  | -  // The 2nd part of the if is working around an iOS 7 bug | 
| 3502 |  | -  // If the SIM card is missing, iOS 7 returns an empty string instead of nil | 
| 3503 |  | -  if (isoCode.length == 0) { | 
| 3504 |  | -    isoCode = NB_UNKNOWN_REGION; | 
| 3505 |  | -  } | 
| 3506 |  | - | 
| 3507 |  | -  return isoCode; | 
| 3508 |  | -} | 
| 3509 |  | - | 
|  | 3464 | +#if !TARGET_OS_TV | 
|  | 3465 | +  return [[CNContactsUserDefaults sharedDefaults].countryCode uppercaseString]; | 
|  | 3466 | +#else | 
|  | 3467 | +  return [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]; | 
| 3510 | 3468 | #endif | 
|  | 3469 | +} | 
| 3511 | 3470 | 
 | 
| 3512 | 3471 | /** | 
| 3513 | 3472 |  * Parses a string and returns it in proto buffer format. This method differs | 
|  | 
0 commit comments