NSArray *subviews = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
for (id child in subviews) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//获取到状态栏码
int networkType = [[child valueForKeyPath:@"dataNetworkType"] intValue];
NSLog(@"networkType = %d",networkType);
switch (networkType) {
case 0:
break;
case 1:
NSLog(@"2G网络");
break;
case 2:
NSLog(@"3G网络");
break;
case 3:
NSLog(@"4G网络");
break;
case 5:
NSLog(@"wifi网络");
break;
default:
break;
}
}
}
实际上通过subViews数组还包括对应的网络提供商和时间以及电池的状态等,都可以从该数组中取到