iOS 获取网络状态,区分2G3G4GWIFI等,非reachability

来源:互联网 发布:拳皇97超强优化版 编辑:程序博客网 时间:2024/05/14 06:14
+(NSString *)getNetWorkStates{
UIApplication *app = [UIApplicationsharedApplication];
NSArray *children = [[[appvalueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
    NSString *state = [[NSStringalloc]init];
    int netType = 0;
//获取到网络返回码
    for (id childin children) {
if ([childisKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            //获取到状态栏
            netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];

            switch (netType) {
                case 0:
                    state = @"无网络";
                    //无网模式
                    break;
                case 1:
                    state = @"2G";
                    break;
                case 2:
                    state = @"3G";
                    break;
                case 3:
                    state = @"4G";
                    break;
                case 5:
                {
                    state = @"WIFI";
                }
                    break;
                default:
                    break;
            }
        }
    }
//根据状态选择
    return state;
}
0 0
原创粉丝点击