NSURLSessionDownloadTask downloadTask:didFinishDownloadingToURL文件不存在?

[英]NSURLSessionDownloadTask downloadTask: didFinishDownloadingToURL file does not exist?

I have implement NSURLSessionDownloadTask for downloading multiple video at a same time. In a normal scenario every thing is working fine. Also background fetch is also working. But when i close the application and restart the application and do the same downloadTask: didFinishDownloadingToURL the temp file path i am getting is wrong. The file does not exist in the path. When i checked the path through finder i found files are exist the only difference is the path provided doest not contain the file name and also there exist and extra folder with the same name of the parent folder where the files should be stored. Please find the path shared below



Can any one help to find the path of the downloaded file or handle such scenarios? I did research on finding a solution but sorry to say this couldn't find it any where


1 个解决方案



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];



© 2014-2018 ITdaan.com 粤ICP备14056181号