没有调用subscriberCellularProviderDidUpdateNotifier [英] subscriberCellularProviderDidUpdateNotifier never being called
问题描述
以下代码:
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
info.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier *carrier) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"User did change SIM");
});
};
内部:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- 或者无论我将代码放在哪里进行测试.
无论我要用iSO 7.1.1在iPad Air Mini Wifi + 3G上更换多少张SIM卡,都不会调用该事件.
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.
我在做什么错了?
推荐答案
您需要牢固引用CTTelephonyNetworkInfo
对象.
Swift(iOS 12.0及更高版本):
在您的应用程序委托类中,为该对象声明一个名为telephonyNetworkInfo
的属性,如下所示:
In your app delegate class, declare a property for this object called telephonyNetworkInfo
like this:
let telephonyNetworkInfo = CTTelephonyNetworkInfo()
然后将其放入应用程序委托的didFinishLaunchingWithOptions
方法中:
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(在iOS 12.0之前):
在您的应用程序委托类中,为该对象声明一个名为telephonyNetworkInfo
的属性,如下所示:
In your app delegate class, declare a property for this object called telephonyNetworkInfo
like this:
let telephonyNetworkInfo = CTTelephonyNetworkInfo()
然后将其放入应用程序委托的didFinishLaunchingWithOptions
方法中:
Then put this in your app delegate's didFinishLaunchingWithOptions
method:
telephonyNetworkInfo.subscriberCellularProviderDidUpdateNotifier = { carrier in
DispatchQueue.main.async {
print("User did change SIM")
}
}
Objective-C(在iOS 12.0之前):
在您的应用程序委托的@interface
(或其类扩展名)中,为该对象声明一个名为telephonyNetworkInfo
的属性,而不是以下内容:
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];
使用此:
self.telephonyNetworkInfo = [[CTTelephonyNetworkInfo alloc] init];
然后将其放入应用程序委托的didFinishLaunchingWithOptions
方法中:
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");
});
};
这篇关于没有调用subscriberCellularProviderDidUpdateNotifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!