![](/img/trans.png)
[英]Is the carrier name returned in iOS a network carrier name or a sim carrier name?
[英]How to read SIM Carrier name programmatically in iOS using Swift4
在深入之前,让我告诉你所有这些 - 是的,我已经浏览了 Stack-Overflow 上提供的所有可能的解决方案。
问题陈述:我无法使用 iPhone 读取可用 SIM 卡的“运营商名称”
我尝试了什么:我尝试了两种不同的解决方案,但无法读取 CarrierName。
解决方案 1:当我尝试此解决方案时,我只收到了 output 的“Carrier” ,而不是 CarrierName。
解决方案1:
//--------------- CodeBase : Solution1 -----------------
let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.serviceSubscriberCellularProviders?.first?.value
if let carrierName = carrier?.carrierName {
cell.textLabel?.text = carrierName
}
else{
cell.textLabel?.text = "No Data"
}
//-------------------------------------------------------
Output:载体
解决方案 2:当我尝试此解决方案时,我收到的“iPhone X”为 output,而不是 CarrierName。
解决方案2:
//--------------- CodeBase : Solution2 -----------------
let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.serviceSubscriberCellularProviders?.first?.value
if var carrierName = carrier?.carrierName {
if carrierName.contains("Carrier"){
carrierName = self.getCarrierName() ?? "No Data"
}
else{
cell.textLabel?.text = carrierName
}
}
else{
cell.textLabel?.text = "No Data"
}
//-------------------------------------------------------
函数:getCarrierName()
//--------------- CodeBase : Part of Solution2 -----------------
func getCarrierName() -> String? {
var carrierName: String?
let typeName: (Any) -> String = { String(describing: type(of: $0)) }
let statusBar = UIApplication.shared.value(forKey: "_statusBar") as! UIView
for statusBarForegroundView in statusBar.subviews {
if typeName(statusBarForegroundView) == "UIStatusBarForegroundView" {
for statusBarItem in statusBarForegroundView.subviews {
if typeName(statusBarItem) == "UIStatusBarServiceItemView" {
carrierName = (statusBarItem.value(forKey: "_serviceString") as! String)
}
}
}
}
return carrierName
}
//-------------------------------------------------------
Output: iPhone X ie 它返回 ModelName 而不是 CarrierName
有人可以帮我获取 - CarrierName。
就我而言( Xcode 11.4, Swift 5.2, iPhone 8, iOS 13.3.1
),我可以获得正确的运营商名称。
代码片段:
import CoreTelephony
if #available(iOS 12.0, *) {
if let providers = CTTelephonyNetworkInfo().serviceSubscriberCellularProviders {
providers.forEach { (key, value) in
print("key: \(key), carrier: \(value.carrierName ?? "nil")")
}
}
} else {
let provider = CTTelephonyNetworkInfo().subscriberCellularProvider
print("carrier: \(provider?.carrierName ?? "nil")")
}
控制台中的结果是:
key: 0000000100000001, carrier: 中国电信
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.