Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subscriberCellularProviderDidUpdateNotifier never being called

Following code:

CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    info.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier *carrier) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"User did change SIM");
        });
    };

Inside:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  • Or no matter where I put the code just to test it.

No matter how many sim card I'm replacing on iPad Air Mini Wifi+3G with iSO 7.1.1 the event never being called.

What am I doing wrong?

like image 516
Idan Moshe Avatar asked Oct 24 '25 04:10

Idan Moshe


1 Answers

You need to hold a strong reference to the CTTelephonyNetworkInfo object.

Swift (iOS 12.0 and newer):

In your app delegate class, declare a property for this object called telephonyNetworkInfo like this:

let telephonyNetworkInfo = CTTelephonyNetworkInfo()

Then put this in your app delegate's didFinishLaunchingWithOptions method:

telephonyNetworkInfo.serviceSubscriberCellularProvidersDidUpdateNotifier = { [weak telephonyNetworkInfo] carrierIdentifier in
    let carrier: CTCarrier? = telephonyNetworkInfo?.serviceSubscriberCellularProviders?[carrierIdentifier]

    DispatchQueue.main.async {
        print("User did change SIM")
    }
}

Swift (Before iOS 12.0):

In your app delegate class, declare a property for this object called telephonyNetworkInfo like this:

let telephonyNetworkInfo = CTTelephonyNetworkInfo()

Then put this in your app delegate's didFinishLaunchingWithOptions method:

telephonyNetworkInfo.subscriberCellularProviderDidUpdateNotifier = { carrier in
    DispatchQueue.main.async {
        print("User did change SIM")
    }
}

Objective-C (Before iOS 12.0):

In your app delegate's @interface (or its class extension), declare a property for this object called telephonyNetworkInfo and instead of this:

CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];

use this:

self.telephonyNetworkInfo = [[CTTelephonyNetworkInfo alloc] init];

And then of course put this in your app delegate's didFinishLaunchingWithOptions method:

self.telephonyNetworkInfo.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier *carrier) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"User did change SIM");
    });
};
like image 78
TylerP Avatar answered Oct 26 '25 17:10

TylerP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!