Can someone confirm that -checkResourceIsReachableAndReturnError:
method of NSURL
is working as expected. I have tried using it for known URLs and it is always returning NO
. I am using XCode's iPhone Simulator 4.1.
Thank you.
3 Answers
According to NSURL Class Reference
Returns whether the resource pointed to by a file URL can be reached.
...
Discussion
This method is unimplemented in iOS, so it performs no operation
So it only works for file URLs (which your URL probably isn't) and it only works on Mac OS X anyway.
-
In conclusion, for iOS, the answer is NO, right? Any workaround? Commented Sep 25, 2010 at 5:26
-
-
I want to see if I have an XML at a specific remote location Commented Oct 23, 2010 at 11:49
-
You could try using NSURLConnection to access the URL and see if you get valid XML out...– tc.Commented Nov 21, 2010 at 6:11
-
-checkResourceIsReachableAndReturnError:
is intended only for local files at present. You can't use it to check the existence of a remote file on any platform Commented Apr 9, 2013 at 15:09
You can use this method:
- (BOOL) validateUrl: (NSString *) url {
NSString *theURL =
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", theURL];
return [urlTest evaluateWithObject:url];
}
-
This doesn't working correctly. Infant, for some valid URLs it returns NO. Commented Feb 11, 2015 at 18:20
If you just want to know if the URL is valid, you can
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"yourstring"]];
bool valid = [NSURLConnection canHandleRequest:req];