Reputation: 1315
I would like to test if a URL exists without loading any actual data.
I was thinking of initiating a NSURLConnection, and then using these two delegate methods to check the status:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Connection failed ... presumably this is a server issue, and I don't know if the URL exists or not.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
int status = [((NSHTTPURLResponse *)response) statusCode];
[connection cancel];
// Do something with the status
}
Would this be a sensible way? The files I'm testing could potentially be very large, so I want to make sure that the actual file is not downloaded.
Thanks, Ron
p.s. I'm looking at adding this because Apple forced my App to stop backing up files downloaded from the internet. Instead, I store the files in non-backed up space, and in my backed up database I keep track of the original location of the file. Since the file is intended to be a permanent part of the user's library, I would like to periodically test if a file is not accessible and then move it into backed up space (so it will survive a restore to a new device, for example). I'm very annoyed at Apple for forcing me to make this change as I can imagine customers losing important data.
p.p.s. For some strange reason, these delegate methods no longer appear in the documentation for NSURLConnectionDelegate Protocol. I'm assuming the documentation is just messed up.
Upvotes: 2
Views: 1767
Reputation: 1401
Personally, I would send a raw request or modify the head to use HEAD which is a standard HTTP header.
It has the same result type as GET, so if you can check the result headers, see if its 403,404 or 201 then you're good.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
Upvotes: 5