ios中判断当前手机的网络状态
typedef enum {
NETWORK_TYPE_NONE= 0,
NETWORK_TYPE_2G= 1,
NETWORK_TYPE_3G= 2,
NETWORK_TYPE_4G= 3,
NETWORK_TYPE_5G= 4,// 5G目前为猜测结果
NETWORK_TYPE_WIFI= 5,
}NETWORK_TYPE;
//方法可以声明成静态方法或者类方法,定义的枚举最好在.h文件中
+(NETWORK_TYPE)getNetworkTypeFromStatusBar {
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
NSNumber *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
dataNetworkItemView = subview;
break;
}
}
NETWORK_TYPE nettype = NETWORK_TYPE_NONE;
NSNumber * num = [dataNetworkItemView valueForKey:@"dataNetworkType"];
nettype = [num intValue];
return nettype;
}