• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Checking if NSURL exists
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Checking if NSURL exists


  • Subject: Re: Checking if NSURL exists
  • From: Jens Alfke <email@hidden>
  • Date: Mon, 05 Aug 2013 09:02:05 -0700

On Aug 5, 2013, at 7:50 AM, Koen van der Drift <email@hidden> 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 (email@hidden)

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:

This email sent to email@hidden


References: 
 >Checking if NSURL exists (From: Koen van der Drift <email@hidden>)

  • Prev by Date: Re: Checking if NSURL exists
  • Next by Date: Re: drawing outside the dirtyRect (of a NSView sub class) and overlapping NSTableView
  • Previous by thread: Re: Checking if NSURL exists
  • Next by thread: Re: Checking if NSURL exists
  • Index(es):
    • Date
    • Thread