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:^{
            
        }];
    }
        
}



推荐阅读更多精彩内容