AFNetworking2.2 通过setReachabilityStatusChangeBlock 检测全局网络连接状态
需要注意的是 [_sharedClient.reachabilityManager startMonitoring];
一定要加上这句话 开启监听block才会工作(写的时候一直不工作,翻了好久不知道原因,官网也没有介绍,结果进到reachabilityManager类中看了看 才发现有这个方法开启监听)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
+ (instancetype)sharedClient { static APIClient *_sharedClient = nil; static dispatch_once_t onceToken; NSString * APIBaseURLString = [NSString stringWithFormat:SERVER_INTERFACES, SERVER_IP]; ; dispatch_once(&onceToken, ^{ _sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:VWAPIBaseURLString]]; [_sharedClient setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]]; [_sharedClient.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusReachableViaWWAN: [Dialog simpleToast:@"网络已连接"]; break; case AFNetworkReachabilityStatusReachableViaWiFi: [Dialog simpleToast:@"网络已连接"]; break; case AFNetworkReachabilityStatusNotReachable: [Dialog simpleToast:@"当前网络不可用,请联系管理员"]; break; default: break; } }]; [_sharedClient.reachabilityManager startMonitoring]; }); return _sharedClient; } |
转载请注明:天狐博客 » iOS AFNetworking2.2 setReachabilityStatusChangeBlock检测网络连接状态