I have the following code:
let networkStatus = CTTelephonyNetworkInfo()
func getCellularInfo() {
if #available(iOS 12.0, *) {
var info = networkStatus.serviceSubscriberCellularProviders
if let aKey = networkStatus.value(forKey: "serviceSubscriberCellularProvider") {
print("aKey: \(aKey)")
}
}
}
这段代码返回以下结果:
我不熟悉这个方法,如何获取与键关联的值,例如aKey: { 0000000100000001 = "CTCarrier (0x28282e610) {\n\t运营商名称: [Vodacom]\n\t移动国家码: [655]\n\t移动网络码:[01]\n\tISO国家码:[za]\n\t允许VOIP? [YES]\n}\n"; }
\n\t移动国家码: [655]/n/
?
value(forKey:)
,请使用object(forKey:)
。另外,根据文档所述,info
是[String: CTCarrier]
,因此您需要获取字典的值(例如,迭代字典并获取运营商),然后使用myCarrier.mobileCountryCode
。 - Larmevalue(forKey:)
呢?直接使用networkStatus.serviceSubscriberCellularProviders
访问值不好吗,就像你已经做的那样? - Alexander