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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

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