简体   繁体   中英

CTTelephonyNetworkInfo returning nil on iOS 12

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

This code retuns:

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

I am not familiar with this method, how do I obtain the values associated with the keys, for example \n\tMobile Country Code: [655]/n/

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

my two cents for 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!"
}

Swift 5 / iOS 15.5 Handling Dual Sim

import CoreTelephony 

let networkStatus = CTTelephonyNetworkInfo()
if let info = networkStatus.serviceSubscriberCellularProviders, let carrier1 = info["0000000100000001"], let carrier2 = info["0000000100000002"] {
   print(carrier1.mobileCountryCode)
   print(carrier1.isoCountryCode)
   print(carrier2.mobileCountryCode)
   print(carrier2.isoCountryCode)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM