[置顶] iOS - 检测网络状态: Reachability和非Reachability

Reachability:

转自:
http://stackoverflow.com/questions/5195012/how-to-use-reachability-class-to-detect-valid-internet-connection

code:

前提:添加框架SystemConfiguration.framework

苹果官方提供了一个叫Reachability的示例程序,便于开发者检测网络状态
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

并且:#import “Reachability.h”

Reachability *reachability = [Reachability reachabilityForInternetConnection];    
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable) {
    //my web-dependent code
}
else {
    //there-is-no-connection warning
}

非Reachability:
code:

 NSURL *url1 = [NSURL URLWithString:@"http://blog.csdn.qxuewei"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url1 cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
        NSHTTPURLResponse *response;
        [NSURLConnection sendSynchronousRequest:request returningResponse: &response error: nil];
        if (response == nil) {
            [MBProgressHUD showError:@"网络错误!"];
        }
        else{
            NSLog(@"网络是通的");

        }

你可能感兴趣的:(ios,框架,网络,苹果)