首页 > 解决方案 > 如何快速下载 Pdf 文件并在文件管理器中查找

问题描述

我已经使用下面的代码下载了 pdf。我可以在 App Data Container 中找到该文件,但从 app Data Container 中我的设备需要 Mac、x 代码或 iTunes 等。

我可以给 Documents 或其他地方在 iPhone 文件中找到 pdf 的区别路径吗?我可以选择使用 iBook 打开文件,但它不存在。

我下载文件的代码在这里:

func downloadFile(){
        let url = "https://www.tutorialspoint.com/swift/swift_tutorial.pdf"
        let fileName = "MyFile"
        
        savePdf(urlString: url, fileName: fileName)
      
    }

    func savePdf(urlString:String, fileName:String) {
            DispatchQueue.main.async {
                let url = URL(string: urlString)
                let pdfData = try? Data.init(contentsOf: url!)
                let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
                let pdfNameFromUrl = "YourAppName-\(fileName).pdf"
                let actualPath = resourceDocPath.appendingPathComponent(pdfNameFromUrl)
                do {
                    try pdfData?.write(to: actualPath, options: .atomic)
                    print("pdf successfully saved!")
    //file is downloaded in app data container, I can find file from x code > devices > MyApp > download Container >This container has the file
                } catch {
                    print("Pdf could not be saved")
                }
            }
        }

标签: iosswiftpdfpreview

解决方案


通过在文件中添加以下行来配置您的应用程序,使其文件出现在“文件”应用程序中Info.plist

<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>

或者

就像下面使用 Xcode

在此处输入图像描述

注意:请记住,您必须运行 iOS 11 或更高版本。


推荐阅读