1.下载苹果提供的包,将其中的Reachability.h 与Reachability.m文件拖到项目中下载地址:Reachability
2.在AppDelegate.m中的didFinishLaunchingWithOptions方法中添加通知:
self.coon = [Reachability reachabilityForInternetConnection];
[self.coon startNotifier];
3.在需要接收网络变化的地方添加监听方法:
//接收网络变化的通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(checkNetworkStatus) name:kReachabilityChangedNotification object:nil];
#pragma mark - 网络变化通知方法
- (void)checkNetworkStatus {
AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NetworkStatus status = [del.coon currentReachabilityStatus];
switch (status) {
case NotReachable:
{
NSLog( @"网络状态(网络不可用)");
}
break;
case ReachableViaWiFi:
{
NSLog( @"网络状态(wifi可用)");
}
break;
case ReachableViaWWAN:{
NSLog( @"网络状态(手机网络可用)");
}
break;
default:
break;
}
}