Swift 从远程url下载urlSession失败-出现CFNetworkDownload_gn6wzc.tmp

Swift 从远程url下载urlSession失败-出现CFNetworkDownload_gn6wzc.tmp,swift,nsurlsession,uidocumentinteraction,nsurlsessiondownloadtask,iosdeployment,Swift,Nsurlsession,Uidocumentinteraction,Nsurlsessiondownloadtask,Iosdeployment,我试图使用urlSession从远程url下载文件。但是当下载完成时,出现了一个名为CFNetworkDownload\u gn6wzc.tmp的意外文件,我无法使用uidocumentinteractioncontroller打开该文件。该文件是什么?如何解决这个问题。谢谢你的回复 这是留言 您的位置是file:///Users/pisal/Library/Developer/CoreSimulator/Devices/26089E37-D4EA-49E5-8D45-BB7D9C52087D/

我试图使用
urlSession
从远程url下载文件。但是当下载完成时,出现了一个名为
CFNetworkDownload\u gn6wzc.tmp
的意外文件,我无法使用
uidocumentinteractioncontroller打开该文件。
该文件是什么?如何解决这个问题。谢谢你的回复

这是留言

您的位置是file:///Users/pisal/Library/Developer/CoreSimulator/Devices/26089E37-D4EA-49E5-8D45-BB7D9C52087D/data/Containers/Data/Application/767E966E-5954-41BA-B003-90E2D27C5558/Library/Caches/com.apple.nsurlsessiond/Downloads/com.SamboVisal.downloadTask/CFNetworkDownload_gn6wzc.tmp

这是我的示例代码:我通过这个方法发送链接

@IBAction func startDownload(_ sender: Any) {
    let url = URL(string: "http://www.tutorialspoint.com/swift/swift_tutorial.pdf")!
    let task = DownloadManager.shared.activate().downloadTask(with: url)
    task.resume()

}
下载完毕后:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        debugPrint("Download finished: \(location)")
        debugPrint("download task : \(downloadTask)")
        ViewController().documentInteraction(location: location)

    }
当我试图打开
uidocumentinteractioncontroller

let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
    let destinationPath = documentDirectoryPath.appendingPathComponent("swift.pdf")
    let destinationUrl = URL(fileURLWithPath: destinationPath)

    do{
        try FileManager.default.copyItem(at: locat, to: destinationUrl)
    }
    catch {
        print("error copying file: \(error.localizedDescription)")
    }

    documentInteractionController.url = destinationUrl

    if !documentInteractionController.presentOpenInMenu(from: openButton.bounds, in: view, animated: true) {
        print("You don't have an app installed that can handle pdf files.")
    }

下载的数据存储在临时文件中。您需要将该数据写入文件并使用它。在
func-urlSession(u-session:urlSession,downloadTask:URLSessionDownloadTask,didfishdownloadingto-location:URL)中编写以下代码
方法:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    debugPrint("Download finished: \(location)")
    do {
        let downloadedData = try Data(contentsOf: location)

        DispatchQueue.main.async(execute: {
            print("transfer completion OK!")

            let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString
            let destinationPath = documentDirectoryPath.appendingPathComponent("swift.pdf")

            let pdfFileURL = URL(fileURLWithPath: destinationPath)
            FileManager.default.createFile(atPath: pdfFileURL.path,
                                           contents: downloadedData,
                                           attributes: nil)

            if FileManager.default.fileExists(atPath: pdfFileURL.path) {
                print("pdfFileURL present!") // Confirm that the file is here!
            }
        })
    } catch {
        print(error.localizedDescription)
    }
}
编辑


当从服务器下载任何文件时,您的应用程序会将所有数据保存在临时文件中。下载该文件后,它会为您提供一个URL,用于临时保存您的数据。现在由您决定如何以及在何处存储这些数据。临时文件不存储在文档目录中。您需要以正确的扩展名将该文件的内容复制到您的文件中。替换代码中给定的方法。当你想阅读该文件时,使用
swift.pdf
而不是tmp文件

下载的数据存储在临时文件中。您需要将该数据写入文件并使用它。在
func-urlSession(u-session:urlSession,downloadTask:URLSessionDownloadTask,didfishdownloadingto-location:URL)中编写以下代码
方法:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    debugPrint("Download finished: \(location)")
    do {
        let downloadedData = try Data(contentsOf: location)

        DispatchQueue.main.async(execute: {
            print("transfer completion OK!")

            let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString
            let destinationPath = documentDirectoryPath.appendingPathComponent("swift.pdf")

            let pdfFileURL = URL(fileURLWithPath: destinationPath)
            FileManager.default.createFile(atPath: pdfFileURL.path,
                                           contents: downloadedData,
                                           attributes: nil)

            if FileManager.default.fileExists(atPath: pdfFileURL.path) {
                print("pdfFileURL present!") // Confirm that the file is here!
            }
        })
    } catch {
        print(error.localizedDescription)
    }
}
编辑


当从服务器下载任何文件时,您的应用程序会将所有数据保存在临时文件中。下载该文件后,它会为您提供一个URL,用于临时保存您的数据。现在由您决定如何以及在何处存储这些数据。临时文件不存储在文档目录中。您需要以正确的扩展名将该文件的内容复制到您的文件中。替换代码中给定的方法。当你想阅读该文件时,使用
swift.pdf
而不是tmp文件

我已经实现了这些方法,但它不会返回pdf文件。它给了我文件
CFNetworkDownload_jJB6ly.tmp
()你能下载我的项目并告诉我它是否有效吗?我已经编辑了我的答案。替换给定的代码。它会起作用的。:)是的,我更换了它,现在它工作了。谢谢你的时间;)。我已经实现了这些方法,但它不会返回pdf文件。它给了我文件
CFNetworkDownload_jJB6ly.tmp
()你能下载我的项目并告诉我它是否有效吗?我已经编辑了我的答案。替换给定的代码。它会起作用的。:)是的,我更换了它,现在它工作了。谢谢你的时间;)。