下载的文件不存在

时间:2015-06-10 08:16:59

标签: ios objective-c

我使用this tutorial

在后台下载文件

下载文件时,有2个功能通知我们。我按时间顺序写下它们:

1) - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL;

2) - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error;

在功能1中,我检查下载的文件是否仍然存在。

在功能2中,我检查下载的文件是否存在。我认为它被iOS系统删除了。​​

你能解释一下为什么吗? 感谢

4 个答案:

答案 0 :(得分:6)

- (void)URLSession:(NSURLSession *)session
      downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location

根据iOS Developer Library

  

session:包含已完成的下载任务的会话。

     

downloadTask:已完成的下载任务。

     

location:临时文件的文件URL。由于该文件是临时文件,因此必须先打开文件进行读取,或者在从此委托方法返回之前将其移动到应用程序沙盒容器目录中的永久位置。   如果您选择打开文件进行读取,则应该在另一个线程中进行实际读取,以避免阻塞委托队列。

是的,下载的文件 临时。如果您想将下载的文件保留在应用中,则应将此数据保存在NSDocuments路径下。

更多信息ray wenderlich's blog

  

任务完成后,调用URLSession:downloadTask:didFinishDownloadingToURL :.这时您可以将文件从临时位置保存到永久位置。

当您使用此方法时,请将下载的数据写入您的文件夹。但是不要阻止线程。

答案 1 :(得分:2)

我遇到了同样的问题。

在致电[downloadURL path]

时,请尝试使用[downloadURL absoluteString]代替fileExistsAtPath:

喜欢这样

BOOL exist1 = [fileManager fileExistsAtPath:[downloadURL path]];

有关详情,请参阅fileExistsAtPath: returning NO for files that exist: - )

答案 2 :(得分:0)

link provided by you中,他们将文件保存在文档目录中,因此您可以在iOS模拟器中通过NSLog语句检查{{3中给出的变量 destinationURL >这应该会帮助你。

NSLog(@"my doc directory path = %@",destinationURL);

答案 3 :(得分:0)

尝试在同一个帖子中复制文件内容。

AudioPlayerViewController: UIViewController, URLSessionDelegate, URLSessionDataDelegate, URLSessionDownloadDelegate{

var defaultSession: URLSession!
    var downloadTask: URLSessionDownloadTask!

func downloadFunc()
{
                defaultSession = Foundation.URLSession(configuration: .default, delegate: self, delegateQueue: nil)
                downloadTask = defaultSession.downloadTask(with: audioUrl)
                downloadTask.resume()
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64,

 totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

    DispatchQueue.main.async {
        self.downloadProgressBar.setProgress(Float(totalBytesWritten)/Float(totalBytesExpectedToWrite), animated: true)

        let progressValue = Int(100 * totalBytesWritten / totalBytesExpectedToWrite)
        self.downloadProgressLabel.text = "\(progressValue)%"
    }
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        do {
            let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

            let destinationUrl = documentsDirectoryURL.appendingPathComponent("destination_path.mp3")

            // after downloading your file you need to move it to your destination url
            try FileManager.default.copyItem(at: location, to: destinationUrl)
            print("File moved to documents folder")

        } catch let error as NSError {
            print(error.localizedDescription)
        }

}