问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/9266154/overcoming-unexpected-behavior-of-nsurls-checkresourceisreachableandreturnerror