why do serviceSubscriberCellularProviders return nil? (in iOS 12)
It's a bug in iOS 12.0.x, and it has fixed in iOS 12.1
CTTelephonyNetworkInfo returning nil on iOS 12
The property serviceSubscriberCellularProviders
on CTTelephonyNetworkInfo
returns a dictionary of CTCarrier
objects keyed by String.
var serviceSubscriberCellularProviders: [String : CTCarrier]?
You can see that in your claimed output: CTCarrier (0x28282e610) {...
.
How you got that output is unclear as your posted code, while syntax correct, never uses the generated info
dictionary variable.
So with correct code (assuming serviceSubscriberCellularProvider
is the key):
let networkStatus = CTTelephonyNetworkInfo()
if let info = networkStatus.serviceSubscriberCellularProviders,
let carrier = info["serviceSubscriberCellularProvider"] {
//work with carrier object
print("MNC = \(carrier.mobileNetworkCode)")
}
But that doesn't seem to work on a single SIM iPhone 7 running iOS 12.0.1. serviceSubscriberCellularProviders
is nil. Possibly the newer phones with dual-SIM hardware will react differently.
The deprecated property still works however.
let networkStatus = CTTelephonyNetworkInfo()
if let carrier = networkStatus.subscriberCellularProvider {
print("MNC = \(carrier.mobileNetworkCode ?? "NO CODE")")
}
Core telephony CTCarrier and isoCountryCode. How to get user's current cellular provider's country?
As the documentation explains:
Note
In this context, the “home” provider is the one with which the user has a cellular plan, as opposed to a roaming provider.
So, you will always get FR, even when roaming, if your home provider is in France.
The reason there are two possible values is that iPhones now support up to two sims; One physical and one e-SIM, so there may be two different cellular providers on a phone.
How can I get details about the device data provider (like Verizon/AT&T) of an iphone programmatically?
You should check the CTCarrier
.
Just import CoreTelephony
into your Swift file.
Then you can use the carrierName
property to get the name of your carrier.
// Setup the Network Info and create a CTCarrier object
let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.subscriberCellularProvider
// Get carrier name
let carrierName = carrier.carrierName
Related Topics
Swift .Uppercasestring or .Lowercasestring Property Replacement
Why Is My Swift Loop Failing with Error "Can't Form Range with End < Start"
Swift How to Cast from Int? to String
Adding Activity Indicator to Uialertview
Generating Random Values in Swift Between Two Integer Values
Insert, Update and Delete Animations with Foreach in Swiftui
How to Add Auto-Complete Comment in Xcode (Swift)
Xcode10 - Dyld: Library Not Loaded for Pod Installed in Framework
How to Quit Swift Repl Without Using Ctrl-D
How to Install Package in Xcode via Swift Package Manager
How to Zip More Than 4 Publishers
Why Is There a Memory Leak at String Creation in Swift
Difference Between Packed VS Normal Data Type