UIApplication *app = [UIApplicationsharedApplication];
NSArray *children = [[[appvalueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
NSString *state = [[NSStringalloc]init];
int netType =0;
//獲取到網絡返回碼
for (id childin children)
{
if ([childisKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")])
{
netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
switch (netType)
{
case0:
state = @"無網絡";
break;
case1:
state = @"2G";
break;
case2:
state = @"3G";
break;
case3:
state = @"4G";
break;
case5:
{
state = @"WIFI";
}
break;
default:
break;
}
}
}