On Aug 5, 2013, at 7:50 AM, Koen van der Drift <koenvanderdr...@gmail.com> 
wrote:

> How can I check if an URL on a remote server is valid and exists, without 
> loading the website first?

That depends on the type of URL. It sounds like you mean HTTP. Basically the 
answer is “you can’t” — the only way to check for existence is to resolve it. 
The most optimal way is to do a HEAD request, which will return only the status 
and headers, not the body.

…Which is what you already figured out in your second post. Your code looks 
basically OK with some caveats:
(a) You’re not checking for errors. Test whether result==nil, and if so examine 
the error. An error here would indicate a networking issue at a lower level 
than HTTP, something like “host unreachable” or “TCP connection reset”.
(b) Don't do synchronous remote-URL loading on the main thread; the call could 
block for up to a minute, and is fairly likely to block for several seconds. If 
you’re populating a menu this way, it’s best to either run asynchronous 
requests, or feed the synchronous requests to dispatch queues in parallel.
(c) If you’re serious about getting up-to-the-minute results, you should 
disable caching.
(d) It’s entirely possible to get a 50x response (i.e. server-side error), or a 
lower-level network error, in which case the answer is neither yes nor no but 
more like “reply hazy; ask again later”. You should consider how you want to 
represent that in your UI.

> I tried [myURL checkResourceIsReachableAndReturnError: &error], but that 
> always returns false, even for a very common URL such as 
> http://www.apple.com/.

As the docs for that method say, "This method is currently applicable only to 
URLs for file system resources. For other URL types, NO is returned.” You have 
to read the fine print with Apple’s URL-based APIs because a number of them 
only apply to “file:” URLs. (This is the drawback of Apple deciding to use URLs 
for filesystem access…)

—Jens
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to