Search code examples
iosobjective-ccore-telephony

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?


Solution

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