下载mp4并保存到应用程序目录

时间:2019-02-27 15:22:29

标签: swift

我下载了mp4并使用以下方法进行保存:

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    let fileManager = FileManager()
    let docDirectoryURL = NSURL(fileURLWithPath: "\(fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0])")
    let destinationFilename = downloadTask.originalRequest?.url?.lastPathComponent
    let destinationURL =  docDirectoryURL.appendingPathComponent("\(destinationFilename!)")
    if let path = destinationURL?.path {
        if fileManager.fileExists(atPath: path) {
            do {
                try fileManager.removeItem(at: destinationURL!)
            } catch let error as NSError {
                print(error.debugDescription)
            }
        }
    }
    do {
        try fileManager.copyItem(at: location, to: destinationURL!)
    }
    catch {
        print("Error while copy file")
        return;
    }
    addDownloadToCoreData(ytk: YTKs[myIndex], saveURL: destinationURL!.absoluteString.replacingOccurrences(of: "file:", with: ""))
}

但是当我执行以下print语句时,它说是false

print(fileManager.fileExists(atPath: (docDirectoryURL.appendingPathComponent("\(destinationFilename!)")?.absoluteString)!))

0 个答案:

没有答案