Skip to content

Commit

Permalink
Keeping a single reference of CTTelephonyNetworkInfo to avoid possibl…
Browse files Browse the repository at this point in the history
…e issues (#521)

* Keeping a single reference of CTTelephonyNetworkInfo to avoid possible issues

This commit fix #444

Resources:
 - #484
 - https://stackoverflow.com/questions/14238586/coretelephony-crash
 - flurry/flurry-ios-sdk#200
 - amplitude/Amplitude-iOS#13

* Keeping only the real changes

Co-authored-by: Bruno Guidolim <bruno@pockid.com>
  • Loading branch information
bguidolim and Bruno Guidolim authored May 4, 2022
1 parent 244f689 commit 6da33df
Showing 1 changed file with 9 additions and 5 deletions.
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

0 comments on commit 6da33df

Please sign in to comment.