Idan Moshe Idan Moshe - 5 months ago 39
iOS Question

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?

Answer Source

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

Swift:

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:

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