检测网络连接状态

http://blog.csdn.net/ysh1251685564/article/details/37526747

#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 ( 80 self . view . frame . size . height  -  70 160 20 )];
    
_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 ];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值