Reachability

一、Reachability中介绍了取得/检测网络状态的方法。

二、使用

    1、添加源文件:Reachability.h和Reachability.m

    2、添加framework———SystemConfiguration.framework

三、网络状态

    Reachability.h定义了三中网络状态

 typedef enum{

        NotReachable = 0,   //无连接

        ReachableViaWiFi,   //使用3G/GPRS网络

        ReachableViaWWAN   //使用WiFi网络

        }NetworkStatus;

 

 

因此可以这样检查网络状态

 

Reachability *r = [Reachability reachabilityWithHostName:@“www.apple.com”];

switch([r currentReachabilityStatus])

{

    case NotReachable:    //没有网络连接

       breakcase ReachableViaWWAN:  //使用3G网络连接

        break;

    case ReachableViaWiFi:    //使用WiFi网络

        break;

}

 

四、检查当前网络环境

  // 是否wifi

    + (BOOL) IsEnableWIFI {
        return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
    }

 



    // 是否3G
   

  + (BOOL) IsEnable3G {
        return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
    }

 


    例子:

    - (void)viewWillAppear:(BOOL)animated {    
    if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable) && 
            ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == NotReachable)) {
            self.navigationItem.hidesBackButton = YES;
            [self.navigationItem setLeftBarButtonItem:nil animated:NO];
        }
    }

 

 

posted @ 2015-05-14 14:27  zhanggui  阅读(377)  评论(0编辑  收藏  举报