Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeping a single reference of CTTelephonyNetworkInfo to avoid possible issues #521

Merged
merged 2 commits into from
May 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions PhoneNumberKit/PhoneNumberKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public final class PhoneNumberKit: NSObject {
let parseManager: ParseManager
let regexManager = RegexManager()

#if os(iOS) && !targetEnvironment(simulator) && !targetEnvironment(macCatalyst)
private static let networkInfo = CTTelephonyNetworkInfo()
#endif

// MARK: Lifecycle

public init(metadataCallback: @escaping MetadataCallback = PhoneNumberKit.defaultMetadataCallback) {
Expand Down Expand Up @@ -294,9 +298,8 @@ public final class PhoneNumberKit: NSObject {
///
/// - returns: A computed value for the user's current region - based on the iPhone's carrier and if not available, the device region.
public class func defaultRegionCode() -> String {
#if os(iOS) && !targetEnvironment(simulator) && !targetEnvironment(macCatalyst)
let networkInfo = CTTelephonyNetworkInfo()
var carrier: CTCarrier? = nil
#if os(iOS) && !targetEnvironment(simulator) && !targetEnvironment(macCatalyst)
var carrier: CTCarrier?
if #available(iOS 12.0, *) {
carrier = networkInfo.serviceSubscriberCellularProviders?.values.compactMap({ $0 }).first
} else {
Expand All @@ -306,7 +309,8 @@ public final class PhoneNumberKit: NSObject {
if let isoCountryCode = carrier?.isoCountryCode {
return isoCountryCode.uppercased()
}
#endif
#endif

let currentLocale = Locale.current
if #available(iOS 10.0, *), let countryCode = currentLocale.regionCode {
return countryCode.uppercased()
Expand All @@ -318,7 +322,7 @@ public final class PhoneNumberKit: NSObject {
return PhoneNumberConstants.defaultCountry
}

/// Default metadta callback, reads metadata from PhoneNumberMetadata.json file in bundle
/// Default metadata callback, reads metadata from PhoneNumberMetadata.json file in bundle
///
/// - returns: an optional Data representation of the metadata.
public static func defaultMetadataCallback() throws -> Data? {
Expand Down