//获取网络类型
+(NSString *)getNetWorkStates{
UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
NSString *state = [[NSString alloc]init];
int netType = 0;
//获取到网络返回码
for (id child in children) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//获取到状态栏
netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
switch (netType) {
case 0:
state = @"无网络";
//无网模式
break;
case 1:
state = @"2G";
break;
case 2:
state = @"3G";
break;
case 3:
state = @"4G";
break;
case 5:
{
state = @"WIFI";
}
break;
default:
break;
}
NSLog(@"%@",state);
}
}
//根据状态选择
return state;
}
但是这个方法有个bug,wifi‘切换到无网络的时候检测不到,希望有大神们有解决的办法告诉我
原文地址:http://blog.csdn.net/yangchen9931/article/details/44936621