iOS7 以下获取网络状态

UIApplication *app = [UIApplication sharedApplication];

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

    //获取到网络返回码

    for (id child in children) {

        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {

            //获取到状态栏

            int netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];

            switch (netType) {

                case 0:

                    //无网模式

                    return @"NULL";

                case 1:

                    return @"2G";

                case 2:

                    return @"3G";

                case 3:

                    return @"4G";

                case 5:

                    return @"WIFI";

            }

        }

    }

    return @"NULL";



其中,我打印了一下children:

<__NSArrayM 0x1740568c0>(

<UIStatusBarSignalStrengthItemView: 0x14de04f60; frame = (6 0; 35 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403eda0>> [Item = <UIStatusBarItem: 0x17403ed80> [UIStatusBarSignalStrengthItemView (Left)]],

<UIStatusBarServiceItemView: 0x14de05af0; frame = (44 0; 50 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403f020>> [Item = <UIStatusBarItem: 0x17403f000> [UIStatusBarServiceItemView (Left)]],

<UIStatusBarDataNetworkItemView: 0x14de06660; frame = (99 0; 13 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403f100>> [Item = <UIStatusBarItem: 0x17403f0a0> [UIStatusBarDataNetworkItemView (Left)]],

<UIStatusBarBatteryItemView: 0x14de06df0; frame = (282 0; 33 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403f240>> [Item = <UIStatusBarItem: 0x17403f1c0> [UIStatusBarBatteryItemView (Right)]],

<UIStatusBarBatteryPercentItemView: 0x14de08f30; frame = (254 0; 25 20); userInteractionEnabled = NO; layer = <CALayer: 0x17403f480>> [Item = <UIStatusBarItem: 0x17403f1e0> [UIStatusBarBatteryPercentItemView (Right)]],

<UIStatusBarIndicatorItemView: 0x14dd03d80; frame = (238 0; 10 20); userInteractionEnabled = NO; layer = <CALayer: 0x170220ca0>> [Item = <UIStatusBarItem: 0x17403f200> [UIStatusBarIndicatorItemView:Alarm (Right)]],

<UIStatusBarIndicatorItemView: 0x14dd046a0; frame = (220 0; 12 20); userInteractionEnabled = NO; layer = <CALayer: 0x170220be0>> [Item = <UIStatusBarItem: 0x17403f220> [UIStatusBarIndicatorItemView:RotationLock (Right)]],

<UIStatusBarTimeItemView: 0x14dd066a0; frame = (145 0; 34 20); userInteractionEnabled = NO; layer = <CALayer: 0x17003d9e0>> [Item = <UIStatusBarItem: 0x17403ed60> [UIStatusBarTimeItemView (Center)]]

)


分别代表信号强度、服务、网络、电池、指示器等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值