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.
Asked
Active
Viewed 6,036 times
1

Mike Abdullah
- 14,933
- 2
- 50
- 75

Interfector
- 1,868
- 1
- 23
- 43
3 Answers
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.

tc.
- 33,468
- 5
- 78
- 96
-
In conclusion, for iOS, the answer is NO, right? Any workaround? – Interfector Sep 25 '10 at 05:26
-
What, exactly, are you trying to achieve? – tc. Sep 26 '10 at 12:28
-
I want to see if I have an XML at a specific remote location – Interfector Oct 23 '10 at 11:49
-
You could try using NSURLConnection to access the URL and see if you get valid XML out... – tc. Nov 21 '10 at 06: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 – Mike Abdullah Apr 09 '13 at 15:09
-
@MikeAbdullah Isn't that what I said? – tc. Apr 10 '13 at 14:05
-
Seems to be in iOS now (since 5.0): [NSURL Class Reference](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html) – JohnK Aug 07 '13 at 15:18
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];
}

sudo rm -rf
- 29,408
- 19
- 102
- 161
-
This doesn't working correctly. Infant, for some valid URLs it returns NO. – Asif Bilal Feb 11 '15 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];

Adam Shiemke
- 3,734
- 2
- 22
- 23