appdelegate中
Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
[reach startNotifier];
需要检测网络状态变化的controller中:
先注册下广播观察者:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
-(void)reachabilityChanged:(NSNotification*)notification{
//NSLog(@"%@",notification
//.userInfo);
Reachability* curReach = [notification object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status != NotReachable)
{
self.datalist=nil;
[_table reloadData];
[self getData];
}
}