判断Iphone,Ipad当前网络状态

来源:互联网 发布:微表情训练软件 编辑:程序博客网 时间:2024/04/29 10:10

1、到苹果管网下载Reachability的Demo,https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

然后把:Reachability.h 和 Reachability.m 添加到工程中


2、使用如下方法就能判断当前是否联网:

bool CCNetworkConfig::isNetworkAvailable()

{

    Reachability *r = [ReachabilityreachabilityWithHostName:@"www.apple.com"];

    switch ([rcurrentReachabilityStatus]) {

        caseReachableViaWWAN// 使用3G网络

            printf("current network 3g/gprs\n");

            return true;

        caseReachableViaWiFi// 使用WiFi网络

            printf("current network wifi\n");

            return true;

        caseNotReachable// 没有网络连接

        default:

            break;

    }

    printf("current network none\n");

    return false;

}