美文网首页otherS
iOS判断当前网络连接状态2G,3G,4G,WIFI等

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

作者: hu9134 | 来源:发表于2017-10-11 13:31 被阅读70次

一般有两种办法来实现:

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 ,也可以通过这个来判断

相关文章

网友评论

    本文标题:iOS判断当前网络连接状态2G,3G,4G,WIFI等

    本文链接:https://www.haomeiwen.com/subject/ichwyxtx.html