iOS判断当前网络连接状态2G,3G,4G,WIFI等

一般有两种办法来实现:

1.使用AFNetWorking:
2.根据状态栏的显示来判断.

但是在iphonex上这个方法报错了(具体原因可以参见另外一篇文章,感谢,非常详细) 文章

- (BOOL)isWWAN {
    
    if (iPhoneX) {
        //这是第一种办法:这里没有必要再去判断是不是iPhoneX,直接使用AFN的判断方法就好了
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWiFi) {
            return YES;
        }else{
            return NO;
        }
    }else{
        UIApplication *app = [UIApplication sharedApplication];
        NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
        int netType = 0;
        for (id child in children) {
            if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
                // 0: 无网络  1:2G 2:3G 3:4G  5:WIFI
                netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
            }
        }
        BOOL WWAN = NO;
        if (netType == 1 || netType == 2 || netType == 3) {
            
            WWAN =  YES;
        }
        return WWAN;
    }

}

还是打印一下现在的statusBar 的subViews

    NSLog(@"%@",[[app valueForKeyPath:@"statusBar"] subviews]);

结果为:

"<_UIStatusBar: 0x7f8f6a402970: regions={\n    bottomCenter = \"<_UIStatusBarRegion: 0x60400013b080: identifier=bottomCenter, enabled=1, displayItems={(\\n)}>\";\n    bottomLeading = \"<_UIStatusBarRegion: 0x60400013b1c0: identifier=bottomLeading, enabled=1, displayItems={(\\n)}>\";\n    expandedCenter = \"<_UIStatusBarRegion: 0x60400013afe0: identifier=expandedCenter, enabled=0, displayItems={(\\n)}>\";\n    expandedLeading = \"<_UIStatusBarRegion: 0x60400013aea0: identifier=expandedLeading, enabled=0, displayItems={(\\n)}>\";\n    expandedTrailing = \"<_UIStatusBarRegion: 0x60400013af40: identifier=expandedTrailing, enabled=0, displayItems={(\\n)}>\";\n    leading = \"<_UIStatusBarRegion: 0x600000139d20: identifier=leading, enabled=1, displayItems={(\\n    <_UIStatusBarDisplayItem: 0x6040000beea0: identifier=_UIStatusBarTimeItem.shortTimeDisplayIdentifier, item=<_UIStatusBarTimeItem: 0x6040006608c0: identifier=_UIStatusBarTimeItem>, view=<_UIStatusBarStringView: 0x7f8f6a4167d0; frame = (8 3.66667; 37.6667 18); text = '9:38 '; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x604000294c80>>>,\\n    <_UIStatusBarDisplayItem: 0x6040000bf140: identifier=_UIStatusBarIndicatorLocationItem, item=<_UIStatusBarIndicatorLocationItem: 0x6040004425e0: identifier=_UIStatusBarIndicatorLocationItem>, view=<_UIStatusBarImageView: 0x7f8f6a418da0; frame = (48.3333 7.33333; 10.6667 10.6667); opaque = NO; userInteractionEnabled = NO; tintColor = UIExtendedGrayColorSpace 1 1; layer = <CALayer: 0x604000235060>>>\\n)}>\";\n    pill = \"<_UIStatusBarRegion: 0x600000139dc0: identifier=pill, enabled=0, displayItems={(\\n)}>\";\n    systemUpdates = \"<_UIStatusBarRegion: 0x60400013ae00: identifier=systemUpdates, enabled=1, displayItems={(\\n)}>\";\n    topTrailing = \"<_UIStatusBarRegion: 0x60400013b260: identifier=topTrailing, enabled=1, displayItems={(\\n)}>\";\n    trailing = \"<_UIStatusBarRegion: 0x60400013ad60: identifier=trailing, enabled=1, displayItems={(\\n    <_UIStatusBarDisplayItem: 0x6040000bf260: identifier=_UIStatusBarCellularItem.signalStrengthDisplayIdentifier, item=<_UIStatusBarCellularItem: 0x604000660b40: identifier=_UIStatusBarCellularItem>, view=<_UIStatusBarCellularSignalView: 0x7f8f6a417320; frame = (0 7.33333; 17 10.6667); userInteractionEnabled = NO; layer = <CALayer: 0x604000234d40>>>,\\n    <_UIStatusBarDisplayItem: 0x6040000bf200: identifier=_UIStatusBarWifiItem.signalStrengthDisplayIdentifier, item=<_UIStatusBarWifiItem: 0x604000441b90: identifier=_UIStatusBarWifiItem>, view=<_UIStatusBarWifiSignalView: 0x7f8f6a413b70; frame = (22 7; 15.3333 11); userInteractionEnabled = NO; layer = <CALayer: 0x604000234920>>>,\\n    <_UIStatusBarDisplayItem: 0x6040000bf3e0: identifier=_UIStatusBarBatteryItem.iconDisplayIdentifier, item=<_UIStatusBarBatteryItem: 0x60400047ff40: identifier=_UIStatusBarBatteryItem>, view=<_UIBatteryView: 0x7f8f6a401cd0; frame = (42.3333 7; 24.3333 11.3333); userInteractionEnabled = NO; layer = <CALayer: 0x604000234640>>>\\n)}>\";\n}, items={\n    \"_UIStatusBarActivityItem_Split\" = \"<_UIStatusBarActivityItem_Split: 0x600000253b90: identifier=_UIStatusBarActivityItem_Split>\";\n    \"_UIStatusBarBackgroundActivityItem\" = \"<_UIStatusBarBackgroundActivityItem: 0x6040004417d0: identifier=_UIStatusBarBackgroundActivityItem>\";\n    \"_UIStatusBarBatteryItem\" = \"<_UIStatusBarBatteryItem: 0x60400047ff40: identifier=_UIStatusBarBatteryItem>\";\n    \"_UIStatusBarBluetoothItem\" = \"<_UIStatusBarBluetoothItem: 0x604000441e00: identifier=_UIStatusBarBluetoothItem>\";\n    \"_UIStatusBarCellularExpandedItem\" = \"<_UIStatusBarCellularExpandedItem: 0x604000660740: identifier=_UIStatusBarCellularExpandedItem>\";\n    \"_UIStatusBarCellularItem\" = \"<_UIStatusBarCellularItem: 0x604000660b40: identifier=_UIStatusBarCellularItem>\";\n    \"_UIStatusBarIndicatorAirPlayItem\" = \"<_UIStatusBarIndicatorAirPlayItem: 0x604000442940: identifier=_UIStatusBarIndicatorAirPlayItem>\";\n    \"_UIStatusBarIndicatorAirplaneModeItem\" = \"<_UIStatusBarIndicatorAirplaneModeItem: 0x604000442970: identifier=_UIStatusBarIndicatorAirplaneModeItem>\";\n    \"_UIStatusBarIndicatorAlarmItem\" = \"<_UIStatusBarIndicatorAlarmItem: 0x604000442580: identifier=_UIStatusBarIndicatorAlarmItem>\";\n    \"_UIStatusBarIndicatorCallForwardingItem\" = \"<_UIStatusBarIndicatorCallForwardingItem: 0x6040004426d0: identifier=_UIStatusBarIndicatorCallForwardingItem>\";\n    \"_UIStatusBarIndicatorLiquidDetectionItem\" = \"<_UIStatusBarIndicatorLiquidDetectionItem: 0x604000442b20: identifier=_UIStatusBarIndicatorLiquidDetectionItem>\";\n    \"_UIStatusBarIndicatorLocationItem\" = \"<_UIStatusBarIndicatorLocationItem: 0x6040004425e0: identifier=_UIStatusBarIndicatorLocationItem>\";\n    \"_UIStatusBarIndicatorQuietModeItem\" = \"<_UIStatusBarIndicatorQuietModeItem: 0x604000442850: identifier=_UIStatusBarIndicatorQuietModeItem>\";\n    \"_UIStatusBarIndicatorRotationLockItem\" = \"<_UIStatusBarIndicatorRotationLockItem: 0x604000442670: identifier=_UIStatusBarIndicatorRotationLockItem>\";\n    \"_UIStatusBarIndicatorTTYItem\" = \"<_UIStatusBarIndicatorTTYItem: 0x604000441ef0: identifier=_UIStatusBarIndicatorTTYItem>\";\n    \"_UIStatusBarIndicatorVPNItem\" = \"<_UIStatusBarIndicatorVPNItem: 0x604000441c50: identifier=_UIStatusBarIndicatorVPNItem>\";\n    \"_UIStatusBarLockItem\" = \"<_UIStatusBarLockItem: 0x604000442730: identifier=_UIStatusBarLockItem>\";\n    \"_UIStatusBarNavigationItem\" = \"<_UIStatusBarNavigationItem: 0x6040004428b0: identifier=_UIStatusBarNavigationItem>\";\n    \"_UIStatusBarThermalItem\" = \"<_UIStatusBarThermalItem: 0x604000441d10: identifier=_UIStatusBarThermalItem>\";\n    \"_UIStatusBarTimeItem\" = \"<_UIStatusBarTimeItem: 0x6040006608c0: identifier=_UIStatusBarTimeItem>\";\n    \"_UIStatusBarWifiItem\" = \"<_UIStatusBarWifiItem: 0x604000441b90: identifier=_UIStatusBarWifiItem>\";\n}>"

可以看出iphonex上的标识为<_UIStatusBarWifiItem: 0x604000441b90: identifier=_UIStatusBarWifiItem>

这个identifier为_UIStatusBarWifiItem ,也可以通过这个来判断

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、前言 iOS11发布也有一段时间了,每次版本升级,相关的适配工作当然是下个版本的核心工作之一。而且这次iOS1...
    景铭巴巴阅读 11,072评论 8 105
  • 总结这几年所遇见的坑 一、 iPhone Size 二、 给navigation Bar 设置 title 颜色 ...
    无沣阅读 539评论 0 0
  • (1)我们此刻的所作所为是否能够对尽早的和持续的交付有价值的软件提供支持? (2)我们的流程是否欢迎变更并能够从变...
    乾丽无限阅读 1,683评论 1 51
  • 我们在人生中少不了选择,可能会经常陷入鱼和熊掌不能兼得的情况,这样会进入短暂的迷茫和焦虑。 能解决的办法除了自己,...
    慧眼识鱼阅读 718评论 2 7
  • iOS项目集成WeexSDK 在iOS工程中集成FrameWork无外乎两种,第一,项目支持cocoaPods,采...
    sweetpf阅读 4,546评论 3 10