【发布时间】:2021-05-19 07:22:20
【问题描述】:
我有一个非常简单的应用程序,它带有一个按钮,可以打开文件应用程序并在控制台中打印 PDF 的内容。这在模拟器上完美运行,但在真正的 iOS 设备上使用该应用时,该应用不会打印任何内容。
import PDFKit
@State var openFile = false
Button(action:{
openFile.toggle()
}){
Image(systemName: "exclamationmark.circle.fill")
}
}
.fileImporter(isPresented: $openFile,
allowedContentTypes: [.pdf],
onCompletion: {result in
if let fileURL = try? result.get() {
print(fileURL)
if let pdf = PDFDocument(url: fileURL) {
print(pdf)
let pageCount = pdf.pageCount
print(pdf.pageCount)
let documentContent = NSMutableAttributedString()
for i in 0 ..< pageCount {
guard let page = pdf.page(at: i) else { continue }
guard let pageContent = page.attributedString else { continue }
documentContent.append(pageContent)
}
print(documentContent.string)
}
}
})
在Info.plist 中,我已经添加了密钥Supports opening documents in place 和Application supports iTunes file sharing,但我只是找不到任何东西让它在真实设备中工作。
提前感谢您的支持!
【问题讨论】:
-
嘿 LeonardoXUI,你能打印出你的文件 URL 吗?那里可能有问题。
-
嗨,乔希,fileURL 打印正确,所以问题不存在。我可以确认问题从
if let pdf = PDFDocument(url: fileURL)行开始。仍然不知道是什么导致它不起作用。它适用于模拟器。 -
我认为您的 PDFDocument 初始化可能无法生成正确的 pdf 对象。您可以将 PDFDocument 初始化代码添加到您的问题中吗?
-
我目前没有初始化程序,因为我遵循的教程没有使用...