700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > iPhone X (XS XR XSMAX)如何根据状态栏上的图标 获取设备的联网状态(不是单个应用的)

iPhone X (XS XR XSMAX)如何根据状态栏上的图标 获取设备的联网状态(不是单个应用的)

时间:2022-01-10 05:03:13

相关推荐

iPhone X (XS XR XSMAX)如何根据状态栏上的图标 获取设备的联网状态(不是单个应用的)

在iPhone X 上通过状态栏获取联网状态使用下面的代码直接闪退

- (BOOL)networkingStatesFromStatebar {UIApplication *app = [UIApplication sharedApplication];NSArray *children = nil;children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];int type = 0;for (id child in children) {if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {type = [[child valueForKeyPath:@"dataNetworkType"] intValue];}}if (type == 0) {return NO;}else{return YES;}}

网上其他的方法

children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];

不崩溃,但是不能获取正确的状态。

因此打印一下,看看能发现什么

UIApplication *app = [UIApplication sharedApplication] ;id _statusBar = [app valueForKeyPath:@"_statusBar"];//UIStatusBar_Modernid _statusBar2 = [_statusBar valueForKeyPath:@"_statusBar"];

打印_statusBar2得到

<_UIStatusBar: 0x102e07e40: regions={bottomLeading = <_UIStatusBarRegion: 0x1d412b220: identifier=bottomLeading, enabled=1, displayItems={()}>,systemUpdates = <_UIStatusBarRegion: 0x1d012c6c0: identifier=systemUpdates, enabled=1, displayItems={()}>,expandedTrailing = <_UIStatusBarRegion: 0x1d412b2c0: identifier=expandedTrailing, enabled=0, displayItems={()}>,leading = <_UIStatusBarRegion: 0x1d012c1c0: identifier=leading, enabled=1, displayItems={(<_UIStatusBarDisplayItem: 0x1d418d8f0: identifier=_UIStatusBarTimeItem.shortTimeDisplayIdentifier, item=<_UIStatusBarTimeItem: 0x1d027ff00: identifier=_UIStatusBarTimeItem>, view=<_UIStatusBarStringView: 0x102e0fd20; frame = (10 3.66667; 33.3333 18); text = '3:42'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x1d4288430>>>,<_UIStatusBarDisplayItem: 0x1d018d340: identifier=_UIStatusBarIndicatorLocationItem, item=<_UIStatusBarIndicatorLocationItem: 0x1d445a370: identifier=_UIStatusBarIndicatorLocationItem>, view=<_UIStatusBarImageView: 0x102f22530; frame = (46 8.33333; 10.6667 10.6667); opaque = NO; userInteractionEnabled = NO; tintColor = UIExtendedGrayColorSpace 0 1; layer = <CALayer: 0x1d422a720>>>)}>,pill = <_UIStatusBarRegion: 0x1d012c260: identifier=pill, enabled=0, displayItems={()}>,expandedLeading = <_UIStatusBarRegion: 0x1d412b360: identifier=expandedLeading, enabled=0, displayItems={()}>,trailing = <_UIStatusBarRegion: 0x1d012c580: identifier=trailing, enabled=1, displayItems={(<_UIStatusBarDisplayItem: 0x1d418e380: identifier=_UIStatusBarCellularItem.signalStrengthDisplayIdentifier, item=<_UIStatusBarCellularItem: 0x1d4670e00: identifier=_UIStatusBarCellularItem>, view=<_UIStatusBarCellularSignalView: 0x102f21580; frame = (0 7.33333; 17 10.6667); userInteractionEnabled = NO; layer = <CALayer: 0x1d003fc20>>>,<_UIStatusBarDisplayItem: 0x1d418ead0: identifier=_UIStatusBarCellularItem.typeDisplayIdentifier, item=<_UIStatusBarCellularItem: 0x1d4670e00: identifier=_UIStatusBarCellularItem>, view=<_UIStatusBarStringView: 0x102f217a0; frame = (21.3333 6; 17 14.3333); text = '4G'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x1d009ebe0>>>,<_UIStatusBarDisplayItem: 0x1d418e5f0: identifier=_UIStatusBarBatteryItem.iconDisplayIdentifier, item=<_UIStatusBarBatteryItem: 0x1d46759c0: identifier=_UIStatusBarBatteryItem>, view=<_UIBatteryView: 0x102e13c50; frame = (42.3333 7; 24.3333 11.3333); userInteractionEnabled = NO; layer = <CALayer: 0x1d422c7a0>>>)}>,topTrailing = <_UIStatusBarRegion: 0x1d412b180: identifier=topTrailing, enabled=1, displayItems={()}>}, items={_UIStatusBarActivityItem_Split = <_UIStatusBarActivityItem_Split: 0x1d466f840: identifier=_UIStatusBarActivityItem_Split>,_UIStatusBarCellularItem = <_UIStatusBarCellularItem: 0x1d4670e00: identifier=_UIStatusBarCellularItem>,_UIStatusBarIndicatorAlarmItem = <_UIStatusBarIndicatorAlarmItem: 0x1d0444320: identifier=_UIStatusBarIndicatorAlarmItem>,_UIStatusBarIndicatorCallForwardingItem = <_UIStatusBarIndicatorCallForwardingItem: 0x1d0444560: identifier=_UIStatusBarIndicatorCallForwardingItem>,_UIStatusBarBackgroundActivityItem = <_UIStatusBarBackgroundActivityItem: 0x1d4670c00: identifier=_UIStatusBarBackgroundActivityItem>,_UIStatusBarBluetoothItem = <_UIStatusBarBluetoothItem: 0x1d027eb00: identifier=_UIStatusBarBluetoothItem>,_UIStatusBarIndicatorRotationLockItem = <_UIStatusBarIndicatorRotationLockItem: 0x1d0444440: identifier=_UIStatusBarIndicatorRotationLockItem>,_UIStatusBarIndicatorQuietModeItem = <_UIStatusBarIndicatorQuietModeItem: 0x1d04445c0: identifier=_UIStatusBarIndicatorQuietModeItem>,_UIStatusBarIndicatorTTYItem = <_UIStatusBarIndicatorTTYItem: 0x1d0442f40: identifier=_UIStatusBarIndicatorTTYItem>,_UIStatusBarCellularExpandedItem = <_UIStatusBarCellularExpandedItem: 0x1d466fc40: identifier=_UIStatusBarCellularExpandedItem>,_UIStatusBarIndicatorLocationItem = <_UIStatusBarIndicatorLocationItem: 0x1d445a370: identifier=_UIStatusBarIndicatorLocationItem>,_UIStatusBarThermalItem = <_UIStatusBarThermalItem: 0x1d0443060: identifier=_UIStatusBarThermalItem>,_UIStatusBarNavigationItem = <_UIStatusBarNavigationItem: 0x1d445d1c0: identifier=_UIStatusBarNavigationItem>,_UIStatusBarIndicatorAirplaneModeItem = <_UIStatusBarIndicatorAirplaneModeItem: 0x1d0443ea0: identifier=_UIStatusBarIndicatorAirplaneModeItem>,_UIStatusBarBatteryItem = <_UIStatusBarBatteryItem: 0x1d46759c0: identifier=_UIStatusBarBatteryItem>,_UIStatusBarWifiItem = <_UIStatusBarWifiItem: 0x1d44588d0: identifier=_UIStatusBarWifiItem>,_UIStatusBarIndicatorVPNItem = <_UIStatusBarIndicatorVPNItem: 0x1d4458c90: identifier=_UIStatusBarIndicatorVPNItem>,_UIStatusBarIndicatorAirPlayItem = <_UIStatusBarIndicatorAirPlayItem: 0x1d445db50: identifier=_UIStatusBarIndicatorAirPlayItem>,_UIStatusBarIndicatorLiquidDetectionItem = <_UIStatusBarIndicatorLiquidDetectionItem: 0x1d4640a80: identifier=_UIStatusBarIndicatorLiquidDetectionItem>,_UIStatusBarTimeItem = <_UIStatusBarTimeItem: 0x1d027ff00: identifier=_UIStatusBarTimeItem>}>

发现了几个关于蜂窝数据与wifi的item

_UIStatusBarCellularItem

_UIStatusBarWifiItem

然后看看如何获取这些Items。

通过xcode设置断点直接查看,得到items里面没有什么可用信息,但是在_displayItemStates有

打印_displayItemStates得到

里面包括状态栏的所有元素,打印一个

_dataEnabled就是数据可用的状态

下面就是判断的代码,

- (BOOL)networkingStatesFromStatebar {if(IsIphoneX){//适配iPhoneXUIApplication *app = [UIApplication sharedApplication] ;NSDictionary *_displayItemStates = [[[app valueForKeyPath:@"_statusBar"] valueForKeyPath:@"_statusBar"] valueForKeyPath:@"_displayItemStates"];////wifi//id value = _displayItemStates[@"_UIStatusBarWifiItem.signalStrengthDisplayIdentifier"];NSLog(@"%@",[value class]);//_UIStatusBarDisplayItemStatebool _dataEnabled =[[value valueForKeyPath:@"_dataEnabled"]boolValue];if (_dataEnabled == YES) {return YES;}////移动数据//value = _displayItemStates[@"_UIStatusBarCellularItem.typeDisplayIdentifier"];bool _dataEnabledCell =[[value valueForKeyPath:@"_dataEnabled"]boolValue];if (_dataEnabledCell == YES) {return YES;}return NO;}else{UIApplication *app = [UIApplication sharedApplication];NSArray *children = nil;children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];int type = 0;for (id child in children) {if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {type = [[child valueForKeyPath:@"dataNetworkType"] intValue];}}if (type == 0) {return NO;}else{return YES;}}

iOS12的状态栏结构有变化需要更改一下(0818更新):

- (BOOL)networkingStatesFromStatebar {if(IsIphoneX){//适配iPhoneXUIApplication *app = [UIApplication sharedApplication] ;NSDictionary *_displayItemStates = [[[app valueForKeyPath:@"_statusBar"] valueForKeyPath:@"_statusBar"] valueForKeyPath:@"_displayItemStates"];////wifi//id value = _displayItemStates[@"_UIStatusBarWifiItem.signalStrengthDisplayIdentifier"];NSLog(@"%@",[value class]);//_UIStatusBarDisplayItemStatebool _dataEnabled =[[value valueForKeyPath:@"_dataEnabled"]boolValue];if (IOS12_OR_LATER) {for (id ieentifi in _displayItemStates.allKeys) {NSString *identifier = [NSString stringWithFormat:@"%@",ieentifi];if ([identifier isEqualToString:@"_UIStatusBarWifiItem.signalStrengthDisplayIdentifier"]) {value = _displayItemStates[ieentifi];_dataEnabled =[[value valueForKeyPath:@"_dataEnabled"]boolValue];}}}if (_dataEnabled == YES) {return YES;}////移动数据//value = _displayItemStates[@"_UIStatusBarCellularItem.typeDisplayIdentifier"];bool _dataEnabledCell =[[value valueForKeyPath:@"_dataEnabled"]boolValue];if (IOS12_OR_LATER) {bool _dataEnabledCell111111 = false;bool _dataEnabledCell222222 = false;for (id ieentifi in _displayItemStates.allKeys) {NSString *identifier = [NSString stringWithFormat:@"%@",ieentifi];//if ([identifier isEqualToString:@"_UIStatusBarCellularCondensedItem.typeDisplayIdentifier"]) {value = _displayItemStates[ieentifi];_dataEnabledCell111111 =[[value valueForKeyPath:@"_dataEnabled"]boolValue];}if ([identifier isEqualToString:@"_UIStatusBarSecondaryCellularExpandedItem.typeDisplayIdentifier"]) {value = _displayItemStates[ieentifi];_dataEnabledCell222222 =[[value valueForKeyPath:@"_dataEnabled"]boolValue];}}_dataEnabledCell = _dataEnabledCell111111 || _dataEnabledCell222222;}if (_dataEnabledCell == YES) {return YES;}return NO;}else{NSLog(@"%@",GlobalVariablesSingle.name);NSLog(@"%d",[[[UIDevice currentDevice] systemVersion] intValue]);//ios12测试版系统崩溃ipadpro 只要ios12的ipad系统都不判断if (([GlobalVariablesSingle.name rangeOfString:@"iPad"].length > 0)&& [[[UIDevice currentDevice] systemVersion] intValue] == 12) {return YES;}UIApplication *app = [UIApplication sharedApplication];NSArray *children = nil;children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];int type = 0;for (id child in children) {if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {type = [[child valueForKeyPath:@"dataNetworkType"] intValue];}}if (type == 0) {return NO;}else{return YES;}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。