在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的状态栏结构有变化需要更改一下(20180818更新):

- (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;}}
}

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

  1. adb隐藏状态栏图标_[应用]华为手机怎么设置隐藏状态栏上的图标

    大家使用手机的时候,有没有觉得这个状态栏图标工具,状态栏图标太拥挤,于是就看到有的小伙伴想把华为手机里的状态栏上的图标进行隐藏了,小版教你隐藏闹钟图标. 先上截图,找找状态栏的亮点吧! 下面就是这款神 ...

  2. Android4.1 如何实现状态栏上信号图标有SIM卡1,2标记,并且当处于2G状态显示“G”,处于3G状态显示“3G”

    [Android Version] 6577/6589 Android V4.1 [Description] 如何实现状态栏上信号图标有SIM卡1,2标记,并且当处于2G状态显示"G&quo ...

  3. iOS机型 iPhone X/XS/XR 判断的5种方式总结

    目前已发布的所有 iPhone 设备的屏幕数据,包括了最新上市的 iPhone XS.iPhone XS Max 和 iPhone XR,请参考<iPhone 屏幕分辨率终极指南>. 最后 ...

  4. iPhone X,XS,XSMax,XR的兼容问题

    设备型号 devicePixelRatio screen.width screen.height 图片选择@3x/2X X/XS 3 375 812 @3x XSMax 3 414 896 @3x X ...

  5. iOS 对iPhone X XS XR XS MAX适配

    勇~敢~的小伙伴儿们,你们好,其实我本来不打算写关于iPhone X的适配的,虽然我已经做好了我们App对于iPhone X的适配,但是真的觉的很鸡肋,可是iPhone X的真机我在前几天感受了一下, ...

  6. 为全面屏iPhone X/XS/XR 创建底部导航栏/底部按钮

    比较图1和图2-左,可以发现,底部按钮在全面屏中并没有足够的空间去展示,理想情况为图2-右以及图3-左,有足够的填充. 图2-左的情况会导致: 1.用户需要双击才能实现按钮的点击 2.有概率会退出到手 ...

  7. android状态栏上显示图标大全,状态栏中的Android应用图标

    对不起,我没有足够的声誉将此添加为评论,但是您是否看过类似的问题?您可以添加所需图标的可绘制资源,并在通知上设置标志,以便允许图标保留在状态栏中. FLAG_NO_CLEAR和FLAG_ONGOING ...

  8. Android获取设备状态栏status bar高度的正确姿势

    Android获取设备状态栏高度的正确姿势 正确代码方式: int height = 0;int resourceId = getApplicationContext().getResources() ...

  9. android+状态栏显示图标大全,Android应用图标在状态栏上显示实现原理

    一.前方 在研究<Android类似360,QQ管家那样的悬浮窗>突发奇想,想把应用的图标也显示到状态栏上,类似手机QQ,而有消息来时改变状态栏上的图标显示. 二.原理 其实很研究完后,才 ...

  10. Android应用图标在状态栏上显示实现原理

    一.前方 在研究< Android类似360,QQ管家那样的悬浮窗>突发奇想,想把应用的图标也显示到状态栏上,类似手机QQ,而有消息来时改变状态栏上的图标显示. 二.原理 其实很研究完后, ...

最新文章

  1. hung-yi lee_p12_深度学习简介
  2. linux的线程要makefile,Linux内核线程之父pid=2的kthreadd线程
  3. 数据结构之堆Heap
  4. 西安电子科技大学计算机录取分数,2021年西安电子科技大学投档线及各省最低录取分数线统计表...
  5. 监听输入 vue_深度对比vue与react创建APP的差别,值得收藏
  6. 看Java大牛是如何高效学习的?你掌握好这些了吗?
  7. java 集合modcount_源码|jdk源码之LinkedList与modCount字段
  8. basler 相机使用出现的问题
  9. EasyUI Tree添加节点
  10. 可方向导不一定连续的例子
  11. Day 32 processthreading_4
  12. 工具说明书 - 网页死链检测工具XENU及其名字来历
  13. 说一下浏览器垃圾回收机制?
  14. Android app集成友盟微社区详细步骤
  15. 自动驾驶仿真软件简介----CARLAGazeboLGSVLOthers
  16. 一个AI小白如何理解近似匹配检索
  17. 【电源专题】什么是开关稳压器
  18. 赚翻,快速带你学会Python爬虫接私单
  19. JSP学习---jsp简介和理解jsp的本质
  20. android 判断文件是否存在

热门文章

  1. java 正则表达式 tab_JAVA 正则表达式 (超详细)
  2. Github注册中,邮箱验证通不过解决办法
  3. 如何在计算机中快速新建TXT文本文档
  4. 预警:Mac木马出新招,利用俄罗斯太空计划作诱饵
  5. Excel VBA 巧用自定义函数进行数组去重
  6. 数字滤波器设计之一:巴特沃斯(Butterworth)滤波器
  7. Houdini10:灯光
  8. em算法 c语言,EM算法原理与应用(附代码)
  9. h3c交换机配置nat_H3C NAT配置实例
  10. 多标签文本分类/三种神经网络/网络模型