Skip to content

Commit 8327e0d

Browse files
authored
Merge pull request iziz#390 from egoldfarb/CoreTelephony->Contacts
Migrate system carrier country code detection to use the Contacts framework. Previously used CoreTelephony framework's CTCarrier has been deprecated will not work starting with XCode 14.3.
2 parents a6f5cb0 + f9428f5 commit 8327e0d

File tree

9 files changed

+32
-64
lines changed

9 files changed

+32
-64
lines changed

Package@swift-5.0.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let package = Package(
2626
.headerSearchPath("Internal")
2727
],
2828
linkerSettings: [
29-
.linkedFramework("CoreTelephony", .when(platforms: [.iOS, .macOS])),
29+
.linkedFramework("Contacts", .when(platforms: [.iOS, .macOS, .watchOS])),
3030
]
3131
),
3232
.testTarget(

Package@swift-5.5.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let package = Package(
2727
.headerSearchPath("Internal")
2828
],
2929
linkerSettings: [
30-
.linkedFramework("CoreTelephony", .when(platforms: [.iOS, .macOS, .macCatalyst])),
30+
.linkedFramework("Contacts", .when(platforms: [.iOS, .macOS, .macCatalyst, .watchOS])),
3131
]
3232
),
3333
.testTarget(

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ And set the **Embedded Content Contains Swift** to "Yes" in your build settings.
5858

5959
#### Setting up manually
6060
Add source files to your projects from libPhoneNumber
61-
- Add "CoreTelephony.framework"
61+
- Add "Contacts.framework"
6262

6363
See sample test code from
6464
> [libPhoneNumber-iOS/libPhoneNumberTests/ ... Test.m] (https://github.com/iziz/libPhoneNumber-iOS/tree/master/libPhoneNumberTests)

libPhoneNumber-iOS.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ DESC
1111
s.authors = { "iziz" => "zen.isis@gmail.com", "hyukhur" => "hyukhur@gmail.com" }
1212
s.source = { :git => "https://github.com/iziz/libPhoneNumber-iOS.git", :tag => s.version.to_s }
1313
s.libraries = 'z'
14-
s.ios.framework = 'CoreTelephony'
14+
s.ios.framework = 'Contacts'
1515
s.ios.deployment_target = "11.0"
1616
s.osx.deployment_target = "10.9"
1717
s.watchos.deployment_target = "4.0"

libPhoneNumber.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
34ACBBAE1B7123090064B3BD /* NBPhoneNumberUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = FD8B84321934C35F00C350EB /* NBPhoneNumberUtil.m */; };
3434
34ACBBAF1B71230B0064B3BD /* NBMetadataHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = FD12C2691A87401B00B53856 /* NBMetadataHelper.m */; };
3535
34ACBBB01B71230E0064B3BD /* NBAsYouTypeFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = FD8B84271934C35F00C350EB /* NBAsYouTypeFormatter.m */; };
36-
34ACBBB51B71241E0064B3BD /* CoreTelephony.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 29F3A15C17B98AE1005D8E07 /* CoreTelephony.framework */; };
3736
34ACBBB61B7124AB0064B3BD /* NBPhoneNumberDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = FD8B842E1934C35F00C350EB /* NBPhoneNumberDefines.h */; settings = {ATTRIBUTES = (Public, ); }; };
3837
34ACBBB71B7124BE0064B3BD /* NBPhoneNumber.h in Headers */ = {isa = PBXBuildFile; fileRef = FD8B842C1934C35F00C350EB /* NBPhoneNumber.h */; settings = {ATTRIBUTES = (Public, ); }; };
3938
34ACBBB81B7124D10064B3BD /* NBNumberFormat.h in Headers */ = {isa = PBXBuildFile; fileRef = FD8B84281934C35F00C350EB /* NBNumberFormat.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -66,6 +65,7 @@
6665
94C9AF6124B3E24900469F54 /* NBGeocoderMetadataHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 944B9B012497ED79007E17B5 /* NBGeocoderMetadataHelper.m */; };
6766
A81D6A2B1BECC44600F68F34 /* NBPhoneNumberDefines.m in Sources */ = {isa = PBXBuildFile; fileRef = A81D6A281BECC43A00F68F34 /* NBPhoneNumberDefines.m */; };
6867
CA55ED89296F51E0005E98A1 /* TestingSource.bundle in Resources */ = {isa = PBXBuildFile; fileRef = CA55ED88296F51E0005E98A1 /* TestingSource.bundle */; };
68+
CAA5E78D29F84B7B00550AA7 /* Contacts.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CAA5E78C29F84B7B00550AA7 /* Contacts.framework */; };
6969
/* End PBXBuildFile section */
7070

7171
/* Begin PBXContainerItemProxy section */
@@ -147,7 +147,6 @@
147147
1485C5251E06F4890092F541 /* NBPhoneNumberUtilTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NBPhoneNumberUtilTest.m; path = libPhoneNumberTests/NBPhoneNumberUtilTest.m; sourceTree = SOURCE_ROOT; };
148148
1485C52B1E06F4930092F541 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = libPhoneNumberTests/Info.plist; sourceTree = SOURCE_ROOT; };
149149
14B7A2931DE9B65D0051AED7 /* libPhoneNumberiOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = libPhoneNumberiOSTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
150-
29F3A15C17B98AE1005D8E07 /* CoreTelephony.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreTelephony.framework; path = System/Library/Frameworks/CoreTelephony.framework; sourceTree = SDKROOT; };
151150
34ACBB851B7122AC0064B3BD /* libPhoneNumberiOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = libPhoneNumberiOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
152151
34ACBB881B7122AC0064B3BD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = "libPhoneNumber-iOS/Info.plist"; sourceTree = "<group>"; };
153152
34ACBB891B7122AC0064B3BD /* libPhoneNumberiOS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = libPhoneNumberiOS.h; path = "libPhoneNumber-iOS/libPhoneNumberiOS.h"; sourceTree = "<group>"; };
@@ -183,6 +182,7 @@
183182
CA2A40602966543D00DBCD69 /* NBTestingMetaData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NBTestingMetaData.h; sourceTree = "<group>"; };
184183
CA2A4061296654E500DBCD69 /* NBTestingMetaData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NBTestingMetaData.h; sourceTree = "<group>"; };
185184
CA55ED88296F51E0005E98A1 /* TestingSource.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = TestingSource.bundle; sourceTree = "<group>"; };
185+
CAA5E78C29F84B7B00550AA7 /* Contacts.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Contacts.framework; path = System/Library/Frameworks/Contacts.framework; sourceTree = SDKROOT; };
186186
FD12C2681A87401B00B53856 /* NBMetadataHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NBMetadataHelper.h; sourceTree = "<group>"; };
187187
FD12C2691A87401B00B53856 /* NBMetadataHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NBMetadataHelper.m; sourceTree = "<group>"; };
188188
FD7A0620167715A0004BBEB6 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
@@ -234,7 +234,7 @@
234234
buildActionMask = 2147483647;
235235
files = (
236236
8B0FD2F31E4A85A70049DF81 /* libz.tbd in Frameworks */,
237-
34ACBBB51B71241E0064B3BD /* CoreTelephony.framework in Frameworks */,
237+
CAA5E78D29F84B7B00550AA7 /* Contacts.framework in Frameworks */,
238238
);
239239
runOnlyForDeploymentPostprocessing = 0;
240240
};
@@ -426,10 +426,10 @@
426426
children = (
427427
8B0FD2F21E4A85A70049DF81 /* libz.tbd */,
428428
FDAD44B418E11B300041825C /* XCTest.framework */,
429+
CAA5E78C29F84B7B00550AA7 /* Contacts.framework */,
429430
FD7A0624167715A0004BBEB6 /* CoreGraphics.framework */,
430431
FD7A0622167715A0004BBEB6 /* Foundation.framework */,
431432
FD7A0620167715A0004BBEB6 /* UIKit.framework */,
432-
29F3A15C17B98AE1005D8E07 /* CoreTelephony.framework */,
433433
);
434434
name = Frameworks;
435435
sourceTree = "<group>";

libPhoneNumber/NBPhoneNumberUtil.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939

4040
- (NSString *)extractPossibleNumber:(NSString *)phoneNumber;
4141
- (NSNumber *)extractCountryCode:(NSString *)fullNumber nationalNumber:(NSString **)nationalNumber;
42-
#if TARGET_OS_IOS
4342
- (NSString *)countryCodeByCarrier;
44-
#endif
4543

4644
- (NSString *)getNddPrefixForRegion:(NSString *)regionCode stripNonDigits:(BOOL)stripNonDigits;
4745
- (NSString *)getNationalSignificantNumber:(NBPhoneNumber *)phoneNumber;

libPhoneNumber/NBPhoneNumberUtil.m

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
#import "NBPhoneNumberDesc.h"
1717
#import "NBRegExMatcher.h"
1818

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"
2221
#endif
2322

2423
static NSString *NormalizeNonBreakingSpace(NSString *aString) {
@@ -52,10 +51,6 @@ @interface NBPhoneNumberUtil ()
5251
@property(nonatomic, strong, readwrite) NBMetadataHelper *helper;
5352
@property(nonatomic, strong, readwrite) NBRegExMatcher *matcher;
5453

55-
#if TARGET_OS_IOS
56-
@property(nonatomic, readonly) CTTelephonyNetworkInfo *telephonyNetworkInfo;
57-
#endif
58-
5954
@end
6055

6156
@implementation NBPhoneNumberUtil
@@ -3452,62 +3447,26 @@ - (NBPhoneNumber *)parse:(NSString *)numberToParse
34523447
}
34533448

34543449
/**
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.
34573452
* For example if you have an AT&T sim card but are in Europe, this will parse the
34583453
* 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.
34603455
*/
34613456
- (NBPhoneNumber *)parseWithPhoneCarrierRegion:(NSString *)numberToParse error:(NSError **)error {
34623457
numberToParse = NormalizeNonBreakingSpace(numberToParse);
34633458

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];
34763460
return [self parse:numberToParse defaultRegion:defaultRegion error:error];
34773461
}
34783462

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-
34983463
- (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];
35103468
#endif
3469+
}
35113470

35123471
/**
35133472
* Parses a string and returns it in proto buffer format. This method differs

libPhoneNumberGeocodingTests/NBPhoneNumberOfflineGeocoderTest.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#import <XCTest/XCTest.h>
1010

1111
#import "NBGeocoderMetadataHelper.h"
12-
#import "NBMetadataHelper.h"
1312
#import "NBPhoneNumber.h"
1413
#import "NBPhoneNumberOfflineGeocoder.h"
1514
#import "NBPhoneNumberUtil.h"

libPhoneNumberTests/NBPhoneNumberParsingPerfTest.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ @implementation NBPhoneNumberParsingPerfTest
4747
#if PERF_TEST
4848

4949
- (void)testParsing {
50-
NSArray *regionCodes = [[NBMetadataHelper CCode2CNMap] allKeys];
50+
NSArray *regionCodes =
51+
[[[[NBMetadataHelper alloc] init] countryCodeToCountryNumberDictionary] allKeys];
5152

5253
NSMutableArray<NBExampleNumber *> *exampleNumbers = [[NSMutableArray alloc] init];
5354

@@ -91,6 +92,17 @@ - (void)testParsing {
9192
}];
9293
}
9394

95+
- (void)testParsingWithDefaultRegion {
96+
NBPhoneNumberUtil *util = [NBPhoneNumberUtil sharedInstance];
97+
98+
NSString *exampleNumber = @"+5491187654321";
99+
[self measureBlock:^{
100+
for (int i = 0; i < 10000; i++) {
101+
[util parseWithPhoneCarrierRegion:exampleNumber error: nil];
102+
}
103+
}];
104+
}
105+
94106
#endif // PERF_TEST
95107

96108
@end

0 commit comments

Comments
 (0)