UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
//获取到网络返回码
for (id child in children) {
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
//获取到状态栏
int netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
switch (netType) {
case 0:
//无网模式
return @"NULL";
case 1:
return @"2G";
case 2:
return @"3G";
case 3:
return @"4G";
case 5:
return @"WIFI";
}
}
}
return @"NULL";
其中,我打印了一下children:
<__NSArrayM 0x1740568c0>(
<UIStatusBarSignalStrengthItemView: 0x14de04f60; frame = (6 0; 35 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403eda0>> [Item = <UIStatusBarItem: 0x17403ed80> [UIStatusBarSignalStrengthItemView (Left)]],
<UIStatusBarServiceItemView: 0x14de05af0; frame = (44 0; 50 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403f020>> [Item = <UIStatusBarItem: 0x17403f000> [UIStatusBarServiceItemView (Left)]],
<UIStatusBarDataNetworkItemView: 0x14de06660; frame = (99 0; 13 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403f100>> [Item = <UIStatusBarItem: 0x17403f0a0> [UIStatusBarDataNetworkItemView (Left)]],
<UIStatusBarBatteryItemView: 0x14de06df0; frame = (282 0; 33 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403f240>> [Item = <UIStatusBarItem: 0x17403f1c0> [UIStatusBarBatteryItemView (Right)]],
<UIStatusBarBatteryPercentItemView: 0x14de08f30; frame = (254 0; 25 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403f480>> [Item = <UIStatusBarItem: 0x17403f1e0> [UIStatusBarBatteryPercentItemView (Right)]],
<UIStatusBarIndicatorItemView: 0x14dd03d80; frame = (238 0; 10 20); userInteractionEnabled = NO; layer = <CALayer: 0x170220ca0>> [Item = <UIStatusBarItem: 0x17403f200> [UIStatusBarIndicatorItemView:Alarm (Right)]],
<UIStatusBarIndicatorItemView: 0x14dd046a0; frame = (220 0; 12 20); userInteractionEnabled = NO; layer = <CALayer: 0x170220be0>> [Item = <UIStatusBarItem: 0x17403f220> [UIStatusBarIndicatorItemView:RotationLock (Right)]],
<UIStatusBarTimeItemView: 0x14dd066a0; frame = (145 0; 34 20); userInteractionEnabled = NO; layer = <CALayer: 0x17003d9e0>> [Item = <UIStatusBarItem: 0x17403ed60> [UIStatusBarTimeItemView (Center)]]
)
分别代表信号强度、服务、网络、电池、指示器等。