views:

103

answers:

2

Hi,

on the iPhone I want to check, if a specific file (test.licence) on a website exsists. My approach (so far) was to download the file and check if it exists in my Documents directory. However, if the file does not exists, Apache presents a HTML error page (which I download instead of test.licence). Checking the file size is a fuzzy solution, since it varies.

So, how do I check, if a online file exists (not more)? I'm looking for a simple yet clean solution.

Regards,

Stefan

+7  A: 

Submit an NSURLRequest via NSURLConnection. When you receive a response, check whether the status code is 200 (it's there), 404 (it's not) or something else (a different thing happened).

Graham Lee
+1  A: 

The 404 error code is the correct way to see if the page exists, but if you're intending on displaying the page in a UIWebView, you're going to want to check if the page exists first, and if it does, then you load it up again within the web view (if you did so first, you'd risk showing the Apache error page).

In order to not use up bandwidth twice, for the first sanity check to see if the file exists, you can do a HEAD request, which grabs only the HTTP headers and not the body of the page. Simply add this to your code (assuming urlRequest is of type NSURLRequest):

[urlRequest setHTTPMethod:@"HEAD"];
Joost Schuur
Smart solution. Thanks ...
Stefan