iOS中如何判断是否有网络

来源:互联网 发布:知商金融官网 编辑:程序博客网 时间:2024/06/02 10:29

Reachability 官方下载链接:http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

在要调用的类里面使用:

<!--StartFragment -->

-(BOOL)testConnection{

    BOOLresult=YES;  

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

    Reachability*r = [Reachability reachabilityWithHostName:@"www.hao123.com"];

    switch ([rcurrentReachabilityStatus]) {

        caseNotReachable:

            NSLog(@"没有网络连接");

            // 没有网络连接

           break;

        caseReachableViaWWAN:

            NSLog(@"使用3G网络");

            // 使用3G网络

           break;

        caseReachableViaWiFi:

            NSLog(@"使用WiFi网络");

            // 使用WiFi网络

            break;

    }    return result;

}
原创粉丝点击