CTTelephonyNetworkInfo 在 iOS 12 上返回 nil

CTTelephonyNetworkInfo returning nil on iOS 12

我有以下代码:

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\tCarrier name: [Vodacom]\n\tMobile Country Code: [655]\n\tMobile Network Code:[01]\n\tISO Country Code:[za]\n\tAllows VOIP? [YES]\n}\n"; }

我不熟悉这种方法,如何获取与键关联的值,例如\n\tMobile Country Code: [655]/n/

CTTelephonyNetworkInfo returns 上的 属性 serviceSubscriberCellularProviders CTCarrier 对象的字典,由 String 键控。

var serviceSubscriberCellularProviders: [String : CTCarrier]?

您可以在声明的输出中看到:CTCarrier (0x28282e610) {...

您如何获得该输出尚不清楚,因为您发布的代码虽然语法正确,但从不使用生成的 info 字典变量。

所以使用正确的代码(假设 serviceSubscriberCellularProvider 是关键):

let networkStatus = CTTelephonyNetworkInfo()
if let info = networkStatus.serviceSubscriberCellularProviders, 
   let carrier = info["serviceSubscriberCellularProvider"] {
    //work with carrier object
    print("MNC = \(carrier.mobileNetworkCode)")
}

但这似乎不适用于单个 SIM iPhone 7 运行 iOS 12.0.1。 serviceSubscriberCellularProviders 为零。可能具有双 SIM 卡硬件的较新手机会有不同的反应。

已弃用的 属性 仍然有效。

let networkStatus = CTTelephonyNetworkInfo()
if let carrier = networkStatus.subscriberCellularProvider {
    print("MNC = \(carrier.mobileNetworkCode ?? "NO CODE")")
}

我的两美分 iOS 13 / swift 5.1

class func getTelephonyInfo()->String?{
    let networkInfo = CTTelephonyNetworkInfo()

    let currCarrierType: String?
    if #available(iOS 12.0, *) {

        let serviceSubscriberCellularProviders = networkInfo.serviceSubscriberCellularProviders

        // get curr value:
        guard let dict = networkInfo.serviceCurrentRadioAccessTechnology else{
            return nil
        }
        // as apple states
        // https://developer.apple.com/documentation/coretelephony/cttelephonynetworkinfo/3024510-servicecurrentradioaccesstechnol
        // 1st value is our string:
        let key = dict.keys.first! // Apple assures is present...

        // use it on previous dict:
        let carrierType = dict[key]

        // to compare:
        guard let carrierType_OLD = networkInfo.currentRadioAccessTechnology else {
            return nil
        }
        currCarrierType = carrierType

    } else {
        // Fall back to pre iOS12
        guard let carrierType = networkInfo.currentRadioAccessTechnology else {
            return nil
        }
        currCarrierType = carrierType
    }


    switch currCarrierType{
    case CTRadioAccessTechnologyGPRS:
        return "2G" + " (GPRS)"

    case CTRadioAccessTechnologyEdge:
        return "2G" + " (Edge)"

    case CTRadioAccessTechnologyCDMA1x:
        return "2G" + " (CDMA1x)"

    case CTRadioAccessTechnologyWCDMA:
        return "3G" + " (WCDMA)"

    case CTRadioAccessTechnologyHSDPA:
        return "3G" + " (HSDPA)"

    case CTRadioAccessTechnologyHSUPA:
        return "3G" + " (HSUPA)"

    case CTRadioAccessTechnologyCDMAEVDORev0:
        return "3G" + " (CDMAEVDORev0)"

    case CTRadioAccessTechnologyCDMAEVDORevA:
        return "3G" + " (CDMAEVDORevA)"

    case CTRadioAccessTechnologyCDMAEVDORevB:
        return "3G" + " (CDMAEVDORevB)"

    case CTRadioAccessTechnologyeHRPD:
        return "3G" + " (eHRPD)"

    case CTRadioAccessTechnologyLTE:
        return "4G" + " (LTE)"

    default:
        break;
    }

    return "newer type!"
}