1

I am downloading files from my server, saving them to device, and displaying them to the user in my app. I want to implement a check to see if the file already exists on the device so we can skip the download and just display, but I can't figure out the best way to do that.

I create a unique fileName for each file and then convert it to an NSURL like this:

NSString *fileString = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:fileName]];
self.fileURL = [[NSURL alloc] initFileURLWithPath:fileString];

Then I write to file and save the URL for use shortly after:

[data writeToFile:fileString atomically:YES];
self.fileURL = [[NSURL alloc] initFileURLWithPath:fileString];

How can I check if that File or URL already exists?

Thanks

Allison
  • 2,213
  • 4
  • 32
  • 56
  • Possible duplicate of http://stackoverflow.com/questions/1927754/testing-file-existence-using-nsurl – Adam Jun 17 '12 at 15:59

1 Answers1

4

NSFileManager has a method to check if a file exists at a path:

[[NSFileManager defaultManager] fileExistsAtPath: fileString]

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsfilemanager_Class/Reference/Reference.html#//apple_ref/doc/uid/20000305-CHDDDDJG

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • Thank you! I had spent over an hour trying to figure this out and in 2 minutes you got it working. Wonderful! –  Jun 17 '12 at 16:04