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当中。