I wonder how I can check if a file exist on a server or not, without downloading the data first.
I have around 30 different objects and some of them is connected to a movie on a server. At the moment I use NSData to control if the the URL exist, and then shows the movie, or if it doesn't and then alerts the user that there is no video for that object. The code I use for the moment:
NSString *fPath = [[NSString alloc] initWithFormat:@"http://www.myserver/%@", [rows idNr]];
NSURL *videoURL = [NSURL URLWithString:fPath];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
url = [NSURL URLWithString:fPath];
[fPath release];
if (videoData) {
[self performSelectorOnMainThread:@selector(playVideo:) withObject:url waitUntilDone:NO];
} else {
NSLog(@"videodata false");
errorLabel.hidden = NO;
activityView.hidden = YES;
}
"rows idNr" is the name of the object. This method is doing what I want, but the problem is that with NSData it first "downloading" the file, and when the URL is validated as a file, the movie is loading once again in the movieplayer. This means that it takes twice as long to load the file.
Suggestions?
Make an URLConnecion object with desired url request and add
NSURLConnectionDelegate
into .h file like I want to check "yoururl" is exist or not then you need to do isand then you can track http status code in delegate function of
NSURLConnectionDelegate
You can also find various status code here.
You could do this by checking the size of the file via an FTP server, using the SIZE command. If the file size is zero then the file simply do not exist.
Check here on how to do this.
You could of course also do this by using a
NSURLRequest
withNSURLConnection
, checking for the status to be either 200 (success) or 404 (failed). The 404 status doesn't have to be that the file doesn't exist though, it could also be that the file just couldn't be retrieved.Here's the code for the accepted answer (for your convenience):
How to make call
It took me a while to dig out my answer to one of the previous questions on this topic. Quote:
This way you’ll know if the file exists and won’t download it all if it does.