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

我已经实现了NSURLSessionDownloadTask,可以同时下载多个视频。在正常情况下,每件事都很好。后台提取也有效。但是,当我关闭应用程序并重新启动应用程序并执行相同的downloadTask:didFinishDownloadingToURL我得到的临时文件路径是错误的。该文件不存在于路径中。当我通过finder检查路径时发现文件存在,唯一的区别是提供的路径不包含文件名,并且还存在与应存储文件的父文件夹名称相同的额外文件夹。请在下面找到共享的路径

/Users/sfm/Library/Developer/CoreSimulator/Devices/EB96B330-4928-422F-8655-DC0E9781014A/data/Containers/Data/Application/54691CE2-D599-41CA-813B-2A8FF7B868F8/Library/Caches/com.apple.nsurlsessiond/Downloads/com.application.tre/com.application.tre

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 个解决方案

#1


1  

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

调试应用程序时,自iOS8以来,每次启动调试会话时文件路径都会更改(至少在模拟器中)。

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

检查路径中的app-identifier,有时在调试时会更改。

Since iOS8, you should use this for fileURLs:

从iOS8开始,你应该将它用于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:

在setDownloadTaskDidFinishDownloadingBlock中:我用它来返回应该保存文件的fileURL:

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/08/07/4470a12adb8fd4eeaee9ff2f0f0592d9.html



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