iOS获取设备网络状态,2G,3G,4G,wifi,非Reachability

来源:互联网 发布:mysql drop column if 编辑:程序博客网 时间:2024/04/28 17:49

 +(NSString *)getStatus

{

     NSArray *children = [[[[UIApplicationsharedApplicationvalueForKeyPath:@"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) 

     {

                 case0:

                     state = @"无网络";

                     //无网模式

                     break;

                 case1:

                     state = @"2G";

                     break;

                 case2:

                     state = @"3G";

                     break;

                 case3:

                     state = @"4G";

                     break;

                 case5:

                 {

                     state = @"WIFI";

                 }

                     break;

                 default:

                 {

                     state = @"未识别网络";

                     break;

                 }

                

             }

         }

     }

     return state;

}

0 0
原创粉丝点击