Reachability檢測網絡狀態

在如今的項目開發當中,監測網絡是否正常很是有必要的。Reachability檢測網絡能夠檢測WiFi 3G 無線局域網網絡

使用Reachabilityserver

下載Reachability,將Reachability添加到項目當中,在要檢測網的類當中添加開發

#import <Reachability.h>it

 

 //  開啓網絡狀態的監聽io

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];class

    

    self.reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];import

    [self.reachability startNotifier];//開始監聽object

 

在方法reachabilityChanged:中寫改變網絡要作的事select

//網絡狀態改變時調用方法下載

-(void)reachabilityChanged:(NSNotification *)note

{

    Reachability *currReach = [note object];

    NSParameterAssert([currReach isKindOfClass:[Reachability class]]);

    

    self.status = [currReach currentReachabilityStatus];

    

    self.isReachable = YES;

    

    switch (self.status) {

        case NotReachable:

        {

            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"網絡狀態" message:@"親!沒有網絡哦!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil];

            [alertView show];

            

            self.isReachable = NO;

        }

            break;

            case ReachableViaWiFi:

        {

            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"網絡狀態" message:@"已鏈接WiFi!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil];

            [alertView show];

            self.isReachable = YES;

        }

            break;

            case ReachableViaWWAN:

        {

            self.isReachable = YES;

        }

            break;

        default:

            break;

    }

}

 

也能夠直接寫在AppDelegate當中。

相關文章
相關標籤/搜索