关于网络状态监听实时,创建单例网络监测(非实时)
#pragma mark 网络状态监测
-(void)checkNetworkStatus{
//创建一个用于测试的url
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
AFHTTPRequestOperationManager *operationManager=[[AFHTTPRequestOperationManager alloc]initWithBaseURL:url];
//根据不同的网络状态改变去做相应处理
[operationManager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
[self alert:@"2G/3G/4G Connection."];
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
[self alert:@"WiFi Connection."];
break;
case AFNetworkReachabilityStatusNotReachable:
[self alert:@"Network not found."];
break;
default:
[self alert:@"Unknown."];
break;
}
}];
//开始监控
[operationManager.reachabilityManager startMonitoring];
}
-(void)checkNetworkStatus{
//创建一个用于测试的url
NSURL *url=[NSURL URLWithString:@"http://www.apple.com"];
AFHTTPSessionManager * operationManager=[[AFHTTPSessionManager alloc]initWithBaseURL:url];
//根据不同的网络状态改变去做相应处理
[operationManager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
// [self alert:@"2G/3G/4G Connection."];
[SVProgressHUD showSuccessWithStatus:@"2G/3G/4G Connection."];
// DNLog(@"2G/3G/4G Connection.");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
[SVProgressHUD showSuccessWithStatus:@"WiFi Connection."];
// DNLog(@"WiFi Connection.");
break;
case AFNetworkReachabilityStatusNotReachable:
[SVProgressHUD showErrorWithStatus:@"Network not found."];
// DNLog(@"Network not found.");
break;
default:
[SVProgressHUD showErrorWithStatus:@"Unknown."];
// DNLog(@"Unknown.");
break;
}
}];
//开始监控
[operationManager.reachabilityManager startMonitoring];
}
/**************** 检测网络状态,并返回无网络状太****************/
要导入头文件
+ (BOOL)netWorkStateFalse {
// Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
// [reach startNotifier];
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable);
}