IOS开发 实时检测网络状态

来源:互联网 发布:淘宝水果供应商 编辑:程序博客网 时间:2024/05/29 21:28

    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 = [[childvalueForKeyPath:@"dataNetworkType"]intValue];

           switch (netType)

            {

               case0:

                    state =@"无网络";

                    break;

               case1:

                    state =@"2G";

                   break;

               case2:

                    state =@"3G";

                   break;

               case3:

                    state =@"4G";

                   break;

               case5:

                {

                    state =@"WIFI";

                }

                   break;

               default:

                   break;

            }

        }

    }