subscriberCellularProviderDidUpdateNotifier never being called

梦想与她 提交于 2020-06-23 03:12:17

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/24121684/subscribercellularproviderdidupdatenotifier-never-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!