目前我们获取手机网络类型主要还是用的 AFNetwork
但是AFNetwork有个问题就是类型不够充分,只能四种类型
typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 2,
};
- AFNetworkReachabilityStatusUnknown 未知网络
- AFNetworkReachabilityStatusNotReachable 无网络
- AFNetworkReachabilityStatusReachableViaWWAN 蜂窝移动网络
- AFNetworkReachabilityStatusReachableViaWiFi WIFI
具体获取代码如下
/**
监测网络类型
*/
-(void)configReachabilityStatusByAFNetwork {
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
[MyAppConfigModel sharedSingleton].netWorkReachabilityStatus = status;
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"⚠️⚠️⚠️⚠️⚠️⚠️⚠️当前网络:未知");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"⚠️⚠️⚠️⚠️⚠️⚠️⚠️当前网络:网络无连接\n请检查网络");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"⚠️⚠️⚠️⚠️⚠️⚠️⚠️当前网络:3G|4G");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"⚠️⚠️⚠️⚠️⚠️⚠️⚠️当前网络:WiFi");
break;
default:
break;
}
}];
}
那么 如何获取蜂窝移动网络的具体类型呢?
这里就需要用到
#import <CoreTelephony/CoreTelephonyDefines.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
具体代码如下
+ (NSInteger)getDeviceNetWorkingStatus{
NSString *state = [[NSString alloc] init];
NSInteger stateType = 0;
AFNetworkReachabilityStatus netWorkReachabilityStatus = [MyAppConfigModel sharedSingleton].netWorkReachabilityStatus;
switch (netWorkReachabilityStatus) {
case AFNetworkReachabilityStatusUnknown:
state = @"无网络";
stateType = 5;
break;
case AFNetworkReachabilityStatusNotReachable:
state = @"无网络";
stateType = 5;
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
{
NSArray *typeStrings2G = @[CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyCDMA1x];
NSArray *typeStrings3G = @[CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD];
NSArray *typeStrings4G = @[CTRadioAccessTechnologyLTE];
CTTelephonyNetworkInfo *teleInfo= [[CTTelephonyNetworkInfo alloc] init];
NSString *accessString = teleInfo.currentRadioAccessTechnology;
if ([typeStrings4G containsObject:accessString]) {
state = @"4G";
stateType = 2;
} else if ([typeStrings3G containsObject:accessString]) {
state = @"3G";
stateType = 3;
} else if ([typeStrings2G containsObject:accessString]) {
state = @"2G";
stateType = 4;
} else {
state = @"无网络";
stateType = 5;
}
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
state = @"WIFI";
stateType = 1;
break;
default:
state = @"无网络";
stateType = 5;
break;
}
NSLog(@"当前网络网络状态 %@",state);
return stateType;
}