shawn.t
shawn.t

Reputation: 105

CTCarrier returning nil on all properties

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?

Upvotes: 3

Views: 1013

Answers (2)

winadiw
winadiw

Reputation: 55

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.

Upvotes: 3

Glenn Posadas
Glenn Posadas

Reputation: 13290

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

Upvotes: 0

Related Questions