我已经使用下面的代码下载了 pdf。我能够在应用程序数据容器中找到该文件,但从应用程序数据容器中我的设备需要 Mac、x 代码或 iTunes 等。
我可以给出文档的不同路径或在 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")
}
}
}
通过将以下行添加到您的应用程序中,配置您的应用程序,使其文件显示在“文件”应用程序中Info.plist
file.
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
OR
就像下面使用 Xcode 一样
注意:请记住,您必须运行 iOS 11 或更高版本。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)