Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:如何以最简单的方式测试互联网连接,而不冻结应用程序(无可达性)?_Ios_Objective C_Reachability_Internet Connection_Sendasynchronousrequest - Fatal编程技术网

iOS:如何以最简单的方式测试互联网连接,而不冻结应用程序(无可达性)?

iOS:如何以最简单的方式测试互联网连接,而不冻结应用程序(无可达性)?,ios,objective-c,reachability,internet-connection,sendasynchronousrequest,Ios,Objective C,Reachability,Internet Connection,Sendasynchronousrequest,在我的代码中,我曾经使用三种方法检查互联网,但它们有局限性: 1/可达性方法: - (BOOL)isInternetOk { Reachability *curReach = [Reachability reachabilityWithHostName:@"apple.com"]; NetworkStatus netStatus = [curReach currentReachabilityStatus]; if (netStatus != NotReachable)

在我的代码中,我曾经使用三种方法检查互联网,但它们有局限性:

1/可达性方法:

- (BOOL)isInternetOk
{
    Reachability *curReach = [Reachability reachabilityWithHostName:@"apple.com"];
    NetworkStatus netStatus = [curReach currentReachabilityStatus];

    if (netStatus != NotReachable) //if internet connexion ok
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
限制:它在大多数情况下都能工作,但我的问题是,如果我连接了一个天线Wi-Fi,但上面没有互联网,它会说连接正常,而事实并非如此。这不是一个好的解决方案,我需要检查状态代码,它似乎不可用的可达性

2/发送同步请求:

- (BOOL)isInternetOk2
{
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
    NSURL* URL = [NSURL URLWithString:@"https://www.google.com"];
    NSError *error = nil;

    [request setURL:URL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:15];

    NSData* response2 = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    if (error)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}
    NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"]];
    request.timeoutInterval = 10;

    [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
         NSLog(@"response status code: %ld, error status : %@", (long)[httpResponse statusCode], error.description);

         if ((long)[httpResponse statusCode] >= 200 && (long)[httpResponse statusCode]< 400)
         {
             // do stuff
             NSLog(@"Connected!");
         }
         else
         {
             NSLog(@"Not connected!");
         }
     }];
限制:它也可以工作,但我的问题是,如果有一个超时,这可能会发生在每一个时刻,它冻结应用程序在太多的时间。如果我把它放在一个线程中,那么当我在dispatch\u async中执行请求时,似乎没有考虑响应

3/sendAsynchronousRequest:

- (BOOL)isInternetOk2
{
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
    NSURL* URL = [NSURL URLWithString:@"https://www.google.com"];
    NSError *error = nil;

    [request setURL:URL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:15];

    NSData* response2 = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    if (error)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}
    NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"]];
    request.timeoutInterval = 10;

    [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
         NSLog(@"response status code: %ld, error status : %@", (long)[httpResponse statusCode], error.description);

         if ((long)[httpResponse statusCode] >= 200 && (long)[httpResponse statusCode]< 400)
         {
             // do stuff
             NSLog(@"Connected!");
         }
         else
         {
             NSLog(@"Not connected!");
         }
     }];
NSOperationQueue*myQueue=[[NSOperationQueue alloc]init];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@]https://www.google.com"]];
request.timeoutInterval=10;
[NSURLConnection sendAsynchronousRequest:请求队列:myQueue completionHandler:^(NSURResponse*响应,NSData*数据,NSError*错误)
{
NSHTTPURLResponse*httpResponse=(NSHTTPURLResponse*)响应;
NSLog(@“响应状态代码:%ld,错误状态:%@”,(长)[httpResponse statusCode],错误。说明);
如果((长)[httpResponse statusCode]>=200&(长)[httpResponse statusCode]<400)
{
//做事
NSLog(@“已连接!”);
}
其他的
{
NSLog(@“未连接!”);
}
}];
限制:我认为这是更好的方法,但我的问题是我必须在代码中的每一个地方都写,这将是一种污染。我想知道是否有一种不那么沉重的方式来做这件事

你觉得怎么样?有没有其他方法更容易检查互联网是否正常工作而不冻结应用程序

提前感谢。

另一个选项

#import <SystemConfiguration/SCNetworkReachability.h>

Nayem是对的-您应该将第三个选项(异步网络检查)包装在一个类方法中,如下所示:

+ (void)checkInternetConnectivityWithSuccessCompletion:(void (^)(void))completion {

NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.google.com"]];
request.timeoutInterval = 10;

[NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
     NSLog(@"response status code: %ld, error status : %@", (long)[httpResponse statusCode], error.description);

     if ((long)[httpResponse statusCode] >= 200 && (long)[httpResponse statusCode]< 400)
     {
         // do stuff
         NSLog(@"Connected!");
         completion();
     }
     else
     {
         NSLog(@"Not connected!");
     }
 }];
}
[YourClass checkInternetConnectivityWithSuccessCompletion:^{
    // your internet is working - add code here
}];

检查这里:&如果您确定第三个是您想要的选项,为什么不将此代码包装到带有完成处理程序的方法中呢?我刚刚尝试过它,它的作用与可达性相同。如果我在没有互联网连接的情况下通过wi-fi天线连接iPad,它会返回true。它应该返回false…哦,完美!我从来没有想过用类方法代替实例方法。谢谢:D