iOS 检测网络状态

导入 Reachability3.0 第三方SDK

导入头文件

#import "Reachability.h"

创建三个按钮

// 按钮1
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"检测站点是否可连接" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(didClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

// 按钮2
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(100, 300, 300, 30);
[btn1 setTitle:@"检测234G是否可连接" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(didClick234G) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];

// 按钮3
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn3.frame = CGRectMake(100, 400, 300, 30);
[btn3 setTitle:@"检测wifi是否可连接" forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(didClickwifi) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3];

第一个按钮的点击事件 — 检测站点是否可连接

// 检测站点是否可连接
-(void)didClick
{
    // 检测站点,通过 Reachability 检测
    Reachability *reach = [Reachability reachabilityWithHostName:@"https://www.taobao.com"];

    // 开始检测
    // 检测reach当前网络状态
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:{  // 无网络状态
            NSLog(@"该站点无法连接。");

            // 提示框
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"该站点无法连接" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:^{

            }];

        }
            break;
        case ReachableViaWiFi:{
            NSLog(@"通过wifi连接站点");

            // 提示框
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"通过wifi连接站点" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:^{

            }];

        }
            break;
        case ReachableViaWWAN:{
            NSLog(@"通过234G连接站点");

            // 提示框
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"通过234G连接站点" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:^{

            }];

        }
            break;

        default:
            break;
    }
}


第二个按钮的点击事件 — 检测234G是否可连接

// 检测234G是否可连接
-(void)didClick234G
{
    Reachability *reach = [Reachability reachabilityForInternetConnection];

    // 判断 234G 的状态
    if ([reach currentReachabilityStatus] != NotReachable) {

        NSLog(@"234G已连接");

        // 提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"234G已连接" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{

        }];

    }else{

        NSLog(@"234G已断开");

        // 提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"234G已断开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{

        }];
    }
}

第三个按钮的点击事件 – 检测wifi是否可连接

// 检测wifi是否可连接
-(void)didClickwifi
{
    Reachability *reach = [Reachability reachabilityForLocalWiFi];

    // 判断
    if ([reach currentReachabilityStatus] != NotReachable) {

        NSLog(@"wifi已连接");

        // 提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"wifi已连接" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{

        }];

    }else{

        NSLog(@"wifi已断开");

        // 提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"wifi已断开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{

        }];


    }

}

wifi状态发生改变时弹出提示框
在 AppDelegate.h 中

// 定义成员变量
{
    Reachability *_reach;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 注册通知,检测wifi状态的改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TongZhi) name:kReachabilityChangedNotification object:nil];

    // 初始化rearch
    _reach = [Reachability reachabilityForLocalWiFi];

    // 开始监听
    [_reach startNotifier];

    return YES;
}


点击事件

// 注册通知方法
-(void)TongZhi
{
    if ([_reach currentReachabilityStatus] == NotReachable) {

        NSLog(@"wifi已经断开");

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"已断开" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alert addAction:action];
        [self.window.rootViewController presentViewController:alert animated:YES completion:^{

        }];

    }else{

        NSLog(@"wifi已连接");

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"已连接" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alert addAction:action];
        [self.window.rootViewController presentViewController:alert animated:YES completion:^{

        }];
    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值