首页 > 代码库 > 检测网络连接状态

检测网络连接状态

#import "AFNetworkReachabilityManager.h"


- (BOOL)isNetWorkReachable{
    
    
__block CGRect rect = _nonNetworkLabel.frame//这里的声明前面加__block,作用是在块里可以修改rect的值;
    
    
AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [afNetworkReachabilityManager 
startMonitoring];  //开启网络监视器;
    
    [afNetworkReachabilityManager 
setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        
switch (status) {
            
case AFNetworkReachabilityStatusNotReachable:{
                
NSLog(@"网络不通:%f", rect.origin.y );
                
_nonNetworkLabel.hidden = NO;
                
_nonNetworkLabel.text = @"网络不通..";
                [
self performSelector:@selector(hidden) withObject:nil afterDelay:3];
                
break;
            }
            
case AFNetworkReachabilityStatusReachableViaWiFi:{
                
NSLog(@"网络通过WIFI连接:%f", rect.origin.y );
                
_nonNetworkLabel.hidden = NO;
                
_nonNetworkLabel.text = @"网络通过WIFI连接..";
                [
self performSelector:@selector(hidden) withObject:nil afterDelay:3];
                
break;
            }
                
            
case AFNetworkReachabilityStatusReachableViaWWAN:{
                
NSLog(@"网络通过流量连接:%f", rect.origin.y );
                
_nonNetworkLabel.hidden = NO;
                
_nonNetworkLabel.text = @"网络通过流量连接..";
                [
self performSelector:@selector(hidden) withObject:nil afterDelay:3];
                
break;
            }
            
default:
                
break;
        }
        
    }];
    
    
    
return [AFNetworkReachabilityManager sharedManager].isReachable;
}

- (
void)hidden
{
    
_nonNetworkLabel.hidden = YES;
}



- (
void)viewDidLoad
{
    [
super viewDidLoad];
    
    
if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) {
        
self.tabBar.translucent = NO;
    }
    
else if ([[[UIDevice currentDevice]systemVersion]floatValue] < 7.0){
        
        
    }
    
//self.view.backgroundColor = [UIColor redColor];

    
//默认选择第几个
    
self.selectedIndex = 0;
    
    
    
self.nonNetworkLabel = [[UILabel alloc]initWithFrame:CGRectMake(80self.view.frame.size.height - 7016020)];
    
_nonNetworkLabel.backgroundColor = [UIColor blackColor];
    
_nonNetworkLabel.alpha = 0.6;
    
_nonNetworkLabel.textColor = [UIColor whiteColor];
    
_nonNetworkLabel.textAlignment = NSTextAlignmentCenter;
    
_nonNetworkLabel.font = [UIFont systemFontOfSize:12];
    
_nonNetworkLabel.hidden = YES;
    [
self.view addSubview:_nonNetworkLabel];
    [
self isNetWorkReachable];
}