使用Reachability網絡監測

使用Reachability監測自己的網絡情況。下面是簡單的測試。很簡單也很實用的一個庫。

資源下載Reachability工程 要的話給[email protected]發封郵件

使用了CocoaPods導入Reachability庫,CocoaPods怎麼用CocoaPods使用

這個簡單的測試用到了常用的方法以及這個庫的常用用法,監測了三種網絡狀態,在這三種網絡狀態下你可以自己處理一些情況。

//
//  ViewController.m
//  RearchAbility
//
//  Created by Wu on 16/3/9.
//  Copyright © 2016年 Wu. All rights reserved.
//

#import "ViewController.h"
#import <Reachability.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //每次用其中一個
    /**
     *  簡單的同步
     */
//    [self syncReachability];
    /**
     *  異步-通知(個人推薦)
     */
//    [self asyncReachNotification];
    /**
     *  異步-塊
     */
    [self asyncReachabilityBlock];
}


- (void)syncReachability {
    Reachability *reach = [Reachability reachabilityForInternetConnection];
//    通過打開一個主頁來判斷網絡情況
//    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    NSLog(@"--->:%d",reach.isReachable);
    NetworkStatus status = [reach currentReachabilityStatus];
    switch (status) {
        case NotReachable:
        {
            NSLog(@"NO NET:%@ | %@",reach.currentReachabilityString,reach.currentReachabilityFlags);
        }
            break;
        case ReachableViaWiFi:
        {
            NSLog(@"WIFI:%@ | %@",reach.currentReachabilityString,reach.currentReachabilityFlags);
        }
            break;
        case ReachableViaWWAN:
        {
            NSLog(@"WWAN:%@ | %@",reach.currentReachabilityString,reach.currentReachabilityFlags);
        }
            break;
            
        default:
        {
            NSLog(@"網絡錯誤!");
        }
            break;
    }
}

/***---***---***---***---***---***---***---***---***---***---***/

- (void)asyncReachNotification {
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];
    [reach startNotifier];<span style="font-family: Arial, Helvetica, sans-serif;">//開始監聽,會啓動一個run loop</span>
}

- (void)reachabilityChanged:(NSNotification *)sender {
    NSLog(@"網絡變化的時候我就顯示哦,麼麼噠");
    //NSNotification有兩個方法:name、object。object指向發通知的對象
    Reachability *reach = [sender object];
    NSString *str;
    switch (reach.currentReachabilityStatus) {
        case NotReachable:
        {
            str = reach.currentReachabilityString;
            //todo what you want when no net
        }
            break;
        case ReachableViaWWAN:
        {
            str = reach.currentReachabilityString;
            //todo what you want when WWAN
        }
            break;
        case ReachableViaWiFi:
        {
            str = reach.currentReachabilityString;
            //todo what you want when WiFi
        }
            break;
        default:
        {
            str = @"這是我不知道的東東狀態";
        }
            break;
    }
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ReachabilityStatus"
                                                   message:str
                                                  delegate:nil
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
    [alert show];
}

/***---***---***---***---***---***---***---***---***---***---***/

- (void)asyncReachabilityBlock {
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    reach.reachableBlock = ^(Reachability * reachability){
        dispatch_async(dispatch_get_main_queue(), ^{
            if (reach.currentReachabilityStatus == ReachableViaWiFi) {
                NSLog(@"WiFi");
                //todo what you want when WiFi
            } else {
                NSLog(@"WWAN");
                //todo what you want when WWAN
            }
            
        });
    };
    reach.unreachableBlock = ^(Reachability * reachability){
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"NO NET");
            //todo what you want when no net
        });
    };
    [reach startNotifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end


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