AFN 无网络监控

时间:2021-02-04 00:14:46

直接把封装到请求里就好

 -(void)afn{
//1.创建网络状态监测管理者
AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
//开启监听,记得开启,不然不走block
[manger startMonitoring];
//2.监听改变
[manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
/*
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 2,
*/
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"没有网络");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G|4G");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi");
break;
default:
break;
}
}];
}