如何从 url 快速下载 Pdf 文件,以及在我设备的文件管理器中的何处找到该文件 [英] How to download a Pdf file in swift from a url, and where to find the file in file manager in my device

查看:52
本文介绍了如何从 url 快速下载 Pdf 文件,以及在我设备的文件管理器中的何处找到该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用下面的代码下载了 pdf,我也可以在 App Data Container 中找到该文件,但是从 App Data Container 中,我的设备需要 Mac、x 代码或 iTunes 等.我的问题是,我可以给出一个区分路径,让我们说文档或在 iPhone 文件中找到 pdf 的位置,我可以选择使用 iBook 打开文件,但它也不存在

I have downloaded the pdf using code below, also I am able to find the file in App Data Container but from app data container my device needs a Mac, x code or iTunes Etc. My question is, can I give a distinction path to let say Documents OR where to find the pdf in iPhone files, I have an option to open the file with iBook but it is not there too

热烈欢迎任何建议我尝试了多个指南和解决方案,但都遇到了同样的问题(文件下载但在 iPhone 中找不到),但其中之一我下载文件的代码在这里对不起,如果问题令人困惑,我是这一切的新手

warmly welcome to any suggestions I have tried multiple guides and solutions getting same problems(File downloads but can't find in iPhone ) but one of my code to download the file is here Sorry if question is confusing, I am new to all this

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")
                }
            }
        }

推荐答案

通过在 Info.plist 文件中添加以下行,配置您的应用,使其文件显示在文件"应用中.

Configure your app so that its files appear in the Files app by adding below lines to your Info.plist file.

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

就像下面使用 Xcode

Just like below using Xcode

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

Note: Remember that you must be running iOS 11 or above.

这篇关于如何从 url 快速下载 Pdf 文件,以及在我设备的文件管理器中的何处找到该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆