Sim card change detection in iOS

2019-07-10 17:01发布

问题:

Somebody please help with the code for sim card detection change in iOS on Swift.

We have an app that lets users authenticate with phone number and we want to detect when user changes his sim(if app is in background) and show alert asking if he wants to authenticate with new number. Basically we want to detect the carrier change of the phone.

In my Appdelegate (DidFinishLaunching method):-

    let info = CTTelephonyNetworkInfo()
    if let cellularProvider  = CTTelephonyNetworkInfo().subscriberCellularProviderDidUpdateNotifier {
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            print("Notify sim change")

        })
    }

This is not detecting sim card change. What am I doing wrong? Please guide.

Thanks In Advance,

回答1:

In app terminate: check APNS devicetoken. If simcard changes then devicetoken changes too. Check with before devicetoken. When as app is running: Use CTTelephonyNetworkInfo().subscriberCellularProviderDidUpdateNotifier.