在项目中,为了好的用户体验,有些场景必须线判断网络状态,然后才能决定改干嘛。比如视频播放,需要线判断是Wifi还是4G,Wifi直接播放,4G先提示用户。获取网络状态的方法大概有三种:
- Reachability
这是苹果的官方演示demo中使用到的方法,我们可以到苹果官方文档里下载Demo(点击左上角Download Sample Code 即可下载),然后把Demo里的Reachability.h和.m考到自己项目中,并在Build Phases 的 Link Binary 添加SystemConfiguration.framework框架,然后在需要使用的地方导入Reachability头文件
在 Reachability头文件中我们可以看到无网络,wifi和蜂窝网三个网络状态的枚举值,如下:
typedef enum : NSInteger {
NotReachable = 0,
ReachableViaWiFi,
ReachableViaWWAN
} NetworkStatus;
(1)获取网络状态:
在iOS 7之前,当用户网络为蜂窝网络时,是没法判断3G、4G等网络类型的,iOS 7 后是可以判断的
-(NSString *)internetStatus {
Reachability *reachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
NSString *net = @"WIFI";
switch (internetStatus) {
case ReachableViaWiFi:
net = @"WIFI";
break;
case ReachableViaWWAN:
net = @"蜂窝数据";
//net = [self getNetType ]; //判断具体类型
break;
case NotReachable:
net = @"当前无网路连接";
default:
break;
}
return net;
}
(2)当用户为手机自带网络时,判断具体网络类型
这时可以使用系统自带的API来判断是2G、3G还是4G,这就要使用到CTTelephonyNetworkInfo这类,记得在工程添加CoreTelephony.framework,并导入头文件
- (NSString *)getNetType
{
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
NSString *currentStatus = info.currentRadioAccessTechnology;
if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"]) {
netconnType = @"GPRS";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyEdge"]) {
netconnType = @"2.75G EDGE";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"]){
netconnType = @"3G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSDPA"]){
netconnType = @"3.5G HSDPA";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSUPA"]){
netconnType = @"3.5G HSUPA";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"]){
netconnType = @"2G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"]){
netconnType = @"3G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"]){
netconnType = @"3G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"]){
netconnType = @"3G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyeHRPD"]){
netconnType = @"HRPD";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"]){
netconnType = @"4G";
}
}
用此方法判断的蜂窝网络类型有如下类型:
CTRadioAccessTechnologyGPRS //介于2G和3G之间,也叫2.5G ,过度技术
CTRadioAccessTechnologyEdge //EDGE为GPRS到第三代移动通信的过渡,EDGE俗称2.75G
CTRadioAccessTechnologyWCDMA
CTRadioAccessTechnologyHSDPA //亦称为3.5G(3?G)
CTRadioAccessTechnologyHSUPA //3G到4G的过度技术
CTRadioAccessTechnologyCDMA1x //3G
CTRadioAccessTechnologyCDMAEVDORev0 //3G标准
CTRadioAccessTechnologyCDMAEVDORevA
CTRadioAccessTechnologyCDMAEVDORevB
CTRadioAccessTechnologyeHRPD //电信使用的一种3G到4G的演进技术, 3.75G
CTRadioAccessTechnologyLTE //接近4G
- 通过系统statusBar判断
这种方法在iPhone X 上可能引起奔溃,可能是苹果修改了statusBar的keyPath,所以拿到的东西为nil,导致崩溃
//获取当前网络状态 (iPhone X 上可能引发崩溃)
+ (NSString *)networkingStatesFromStatebar {
// 状态栏是由当前app控制的,首先获取当前app
UIApplication *app = [UIApplication sharedApplication];
//iphone X 会在这里崩溃
NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
int type = 0;
for (id child in children) {
if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
}
}
NSString *stateString = @"wifi";
switch (type) {
case 0:
stateString = @"notReachable";
break;
case 1:
stateString = @"2G";
break;
case 2:
stateString = @"3G";
break;
case 3:
stateString = @"4G";
break;
case 4:
stateString = @"LTE";
break;
case 5:
stateString = @"wifi";
break;
default:
break;
}
return stateString;
}
- 通过AFNetworking
此方法可以写在AppDelegate中,每当网络状态发生变化时,就可以发通知,实时监测网络变化,然后做相应的处理
#pragma mark - ------------- 监测网络状态 -------------
- (void)monitorNetworking
{
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case -1:
NSLog(@"未知网络");
break;
case 0:
NSLog(@"网络不可达");
break;
case 1:
{
NSLog(@"GPRS网络");
//发通知,带头搞事
[[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"1" userInfo:nil];
}
break;
case 2:
{
NSLog(@"wifi网络");
//发通知,搞事情
[[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"2" userInfo:nil];
}
break;
default:
break;
}
if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
NSLog(@"有网");
}else{
NSLog(@"没网");
}
}];
}