Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overcoming unexpected behavior of NSURL's checkResourceIsReachableAndReturnError

Tags:

ios

nsurl

In a program I am creating for iOS 5, I am experiencing unexpected behavior with the checkResourceIsReachableAndReturnError method of NSURL.

I created a new project to verify the issue and included only the code:

NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
NSError *err = nil;
if([url checkResourceIsReachableAndReturnError:&err]){
  NSLog(@"URL is reachable");
}else {
 NSLog(@"URL is not reachable");
}

Further, I tried:

NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
NSError *err = nil;
[url checkResourceIsReachableAndReturnError:&err];
if(err == nil){
 NSLog(@"URL is reachable");
}else {
 NSLog(@"URL is not reachable");
}

...still to no avail. The result is always, "URL is not reachable", contrary to stackoverflow.com (and other domains I tested) obviously being reachable. How does one utilize this function to check if a resource is reachable?

like image 281
Mat Kelly Avatar asked Feb 13 '12 18:02

Mat Kelly


1 Answers

The Apple docs for checkResourceIsReachableAndReturnError: state:

Returns whether the resource pointed to by a file URL can be reached.

Note: "file URL", not Internet or other scheme URL.

like image 156
zaph Avatar answered Nov 12 '22 01:11

zaph