iOS开发之CoreTelephoney框架的应用
CoreTelephoney框架用来获取手机网络状态以及运营商相关信息。
一、CTTelephonyNetworkInfo类
这个类是CoreTelephoney框架的核心,使用它来获取手机的运营商、网络等状态信息。使用示例如下:
1 2 3 4 5 6 7
| - (void)viewDidLoad { [super viewDidLoad]; CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init]; CTCarrier *carrier = info.subscriberCellularProvider; NSLog(@"carrier:%@", [carrier description]); }
|
运营商信息示例如下:
1 2 3 4 5
| Carrier name: [中国移动] Mobile Country Code: [460] Mobile Network Code:[02] ISO Country Code:[cn] Allows VOIP? [YES]
|
CTTelephonyNetworkInfo类解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @property(readonly, retain, nullable) NSDictionary<NSString *, CTCarrier *> *serviceSubscriberCellularProviders;
@property(readonly, retain, nullable) CTCarrier *subscriberCellularProvider;
@property (nonatomic, readonly, retain, nullable) NSDictionary<NSString *, NSString *> * serviceCurrentRadioAccessTechnology;
@property (nonatomic, readonly, retain, nullable) NSString* currentRadioAccessTechnology;
|
CTCattier类中定义了运营商相关的信息,解析如下:
1 2 3 4 5 6 7 8 9 10
| @property (nonatomic, readonly, retain, nullable) NSString *carrierName;
@property (nonatomic, readonly, retain, nullable) NSString *mobileCountryCode;
@property (nonatomic, readonly, retain, nullable) NSString *mobileNetworkCode;
@property (nonatomic, readonly, retain, nullable) NSString* isoCountryCode;
@property (nonatomic, readonly, assign) BOOL allowsVOIP;
|
CTCellularData类用来监听用户的网络状态,可以设置当网络状态发生变化后回调的方法,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| cellularData = [[CTCellularData alloc] init]; cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState restrictedState) { switch (restrictedState) { case kCTCellularDataRestrictedStateUnknown: NSLog(@"蜂窝移动网络状态:未知"); break; case kCTCellularDataRestricted: NSLog(@"蜂窝移动网络状态:关闭"); break; case kCTCellularDataNotRestricted: NSLog(@"蜂窝移动网络状态:开启"); break; default: break; } };
|
需要注意,在iOS中使用网络需要获取用户权限,如果用户没有给网络权限,获取到的状态也将是未开启。
二、CTCallCenter
使用CTCallCenter相关类可以获取当前通话电话的相关信息,CTCallCenter通过管理中心,其中提供了一个方法来获取当前进行中的通话:
1 2
| @property(readonly, retain, nullable) NSSet<CTCall*> *currentCalls;
|
通话被抽象成CTCall对象,解析如下:
1 2 3 4 5 6 7 8 9 10
|
@property(nonatomic, readonly, copy) NSString *callState;
@property(nonatomic, readonly, copy) NSString *callID;
|