1

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 3

5

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.

7
  • In conclusion, for iOS, the answer is NO, right? Any workaround? Commented Sep 25, 2010 at 5:26
  • What, exactly, are you trying to achieve?
    – tc.
    Commented Sep 26, 2010 at 12:28
  • 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
4

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];
   }
1
  • This doesn't working correctly. Infant, for some valid URLs it returns NO.
    – Asif Bilal
    Commented Feb 11, 2015 at 18:20
1

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];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.