网络状态

监听网络连接状态

使用AFNetWorking框架中的AFNetworkReachabilityManager实现。

使用下面的代码,可以监听设备的网络连接状态。

AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager manager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    // do something
}];
[manager startMonitoring];

使用下面的代码,可以监听设备针对特定域名或者ip的连接状态。

//AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager managerForDomain:@"www.xxx.com"];
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager managerForAddress:@"xx.xx.xx.xx"];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    // do something
}];
[manager startMonitoring];

判断蜂窝数据类型

CTTelephonyNetworkInfo *current = [[CTTelephonyNetworkInfo alloc] init];
NSString *tempst = current.currentRadioAccessTechnology;
CTCarrier *tempca = current.subscriberCellularProvider;
NSLog(@"\n%@\n%@", tempst, tempca);
//我的电信手机输出为:
CTRadioAccessTechnologyLTE
CTCarrier (0x170058300) {
    Carrier name: [中国电信]
    Mobile Country Code: [460]
    Mobile Network Code:[11]
    ISO Country Code:[cn]
    Allows VOIP? [YES]
}

currentRadioAccessTechnology 参数详解

CTRadioAccessTechnologyGPRS             2.5G
CTRadioAccessTechnologyEdge             2.75G

CTRadioAccessTechnologyWCDMA            3G
CTRadioAccessTechnologyHSDPA            3.5G
CTRadioAccessTechnologyHSUPA            3.5G

CTRadioAccessTechnologyCDMA1x           2G
CTRadioAccessTechnologyCDMAEVDORev0     3G
CTRadioAccessTechnologyCDMAEVDORevA     3.5G
CTRadioAccessTechnologyCDMAEVDORevB     3.5G
CTRadioAccessTechnologyeHRPD            3G 高通对EVDO的统称

CTRadioAccessTechnologyLTE              4G

你可能感兴趣的:(网络状态)