IOS - 总结(网络状态变更)
- (void)initNetworkMonitor
{
NSURL *baseURL = [NSURL URLWithString:@"http://www.baidu.com/"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
NSOperationQueue *operationQueue = manager.operationQueue;
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
static AFNetworkReachabilityStatus lastStatus = 2;
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
[operationQueue setSuspended:NO];
if (lastStatus == -1 || lastStatus == 0) {
[self showAlertTitle:@"提醒:" message:@"当前网络已连接,网络状态良好!"];
}
break;
case AFNetworkReachabilityStatusNotReachable:
default:
[operationQueue setSuspended:YES];
if (lastStatus == 1 || lastStatus == 2) {
[self showAlertTitle:@"提醒:" message:@"当前网络不可用,请请稍后重试!"];
}
break;
}
lastStatus = status;
}];
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。