CTCarrier returning nil on all properties

Viewed 708

I'm making use of the following code to obtain the mobileNetworkCode:

let ctCarrier = CTCarrier()

    func getMNC() -> String {
        if let mnc = ctCarrier.mobileNetworkCode {
            return mnc
        }
        return "nil"
    }

Using any properties of CTCarrier returns nil and seems like a bug from Apple.

Any workaround as to this problem?

2 Answers
let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.subscriberCellularProvider
let mobileNetworkCode = carrier!.mobileNetworkCode

if mobileNetworkCode != nil {
    //Means got reception
} else {
   //No cellular data
}

Please try this way, i also test using CTCarrier() directly, it always returns nil. This method works.

Make sure you have the following:

  1. You're using a real device when debugging.
  2. It has a sim card.
  3. Device has a signal.
  4. Not on Airplane Mode.

Discussion on mobileNetworkCode:

A read-only NSString object that represents the numeric mobile network code for the user’s cellular service provider. Typing this property as an NSString object, rather than a number type, ensures that leading zeroes in MNCs are respected.

Reference: https://developer.apple.com/documentation/coretelephony/ctcarrier/1620324-mobilenetworkcode

Related