CTTelephonyNetworkInfo在iOS 12上返回nil

时间:2018-10-30 14:12:38

标签: ios swift key-value core-telephony

我有以下代码:

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吗?[是] \ n} \ n“;   }

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

2 个答案:

答案 0 :(得分:2)

serviceSubscriberCellularProviders上的属性CTTelephonyNetworkInfo返回由String键控的CTCarrier个对象的字典。

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)")
}

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

已弃用的属性仍然可以使用。

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

答案 1 :(得分:2)

我为iOS 13 / Swift 5.1支付2美分

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!"
}