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:^{

        }];
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章