Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exists at particular URL?

How do I check if a file exists on a web site? I am using NSURLConnection with my NSURLRequest and an NSMutableData object to store what comes back in the didReceiveData: delegate method. In the connectionDidFinishingLoading: method I then save the NSMutableData object to the file system. All good. Except: if the file does not exist at the website, my code still runs, gets data and saves a file.

How can I check the file is there before I make download request?

like image 408
Magic Bullet Dave Avatar asked Dec 03 '25 03:12

Magic Bullet Dave


1 Answers

Implement connection:didReceiveResponse:, which will be called before connection:didReceiveData:.

The response should be an NSHTTPURLResponse object — suppose you're issuing an HTTP request. You can therefore check [response statusCode] == 404 to determine if the file exists or not.

See also Check if NSURL returns 404.

like image 187
kennytm Avatar answered Dec 04 '25 20:12

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!