码迷,mamicode.com
首页 > 其他好文 > 详细

检测网络连接状态

时间:2014-07-08 14:01:55      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:网络   流量   网络状态   

#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];
}

检测网络连接状态,布布扣,bubuko.com

检测网络连接状态

标签:网络   流量   网络状态   

原文地址:http://blog.csdn.net/ysh1251685564/article/details/37526747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!