IOS Reachability库监听网络状态

来源:互联网 发布:软件产业基地1栋 编辑:程序博客网 时间:2024/05/21 22:25

首先需要导入Reachability.h ...m 2个文件,然后在init注册监听

 #import "Reachability.h"
 //注册网络状态改变通知        [[NSNotificationCenter defaultCenter] addObserver:self                                               selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];        hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"] retain];//可以以多种形式初始化        [hostReach startNotifier];  //开始监听,会启动一个run loop  

 -(void)reachabilityChanged:(NSNotification *)note{    NetworkStatus status = [hostReach currentReachabilityStatus];    if (status == NotReachable){        NSLog(@"网络不可用哦");        isNetworkAvalible = NO;    }else{        NSLog(@"网络可用了哦");        isNetworkAvalible = YES;        [self startMonitor];    }}<br xmlns="http://www.w3.org/1999/xhtml" />这样就可以动态监测程序内的网络变化了。


这样就可以动态监测程序内的网络变化了。

相关请看:

http://stackoverflow.com/questions/27310465/detecting-network-connectivity-changes-using-reachability-nsnotification-and-ne

http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability


0 0
原创粉丝点击