两种方法监测网络状态:
- AFNetworking中AFNetworkReachabilityManager监测网络状态
(void) inspectNetworkState {
//创建网络状态监测管理者
AFNetworkReachabilityManager manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
/
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 2,
*/
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知");
[self showMessage:@"未知网络" duration:4];
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"没有网络");
[self showMessage:@"当前网络不可用,请检查您的网络设置" duration:4];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G|4G");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi");
break;
default:
break;
}
}];
//开始监控
[manager startMonitoring];
}/