NSURLSessionDownloadTask downloadTask: didFinishDownloadingToURL file does not exist? NSURLSessionDownloadTask downloadTask: didFinishDownloadingToURL file does not exist? objective-c objective-c

NSURLSessionDownloadTask downloadTask: didFinishDownloadingToURL file does not exist?


When debugging an app, since iOS8, file paths change every time you start a debug session (at least in the Simulator).

Check the app-identifier in the path, it sometimes changes when you debug it.

Since iOS8, you should use this for fileURLs:

NSURL *documentsPath = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];

In setDownloadTaskDidFinishDownloadingBlock: I use this to return the fileURL where it should save the file:

return [documentsPath URLByAppendingPathComponent:fileName];

To open the file:

NSURL *fileURL = [documentsPath URLByAppendingPathComponent:fileName];

To delete the file:

NSURL *fileURL = [documentsPath URLByAppendingPathComponent:fileName];NSError *error = nil;[[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];