Swift - 在模拟器中获取 PDF 数据,但在真实环境中却不行 iPhone
Swift - Getting PDF data working in Simulator but not on a real iPhone
我有一个非常简单的应用程序,它有一个按钮可以打开文件应用程序并在控制台中打印 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
,但我找不到任何东西可以让它在真实设备上工作。
在此先感谢您的支持!
找到答案了!
将 if let
更改为 guard let
添加了 fileURL.startAccessingSecurityScopedResource()
和 fileURL.stopAccessingSecurityScopedResource()
Button(action:{
openFile.toggle()
}){
Image(systemName: "exclamationmark.circle.fill")
}
}.fileImporter(
isPresented: $openFile,
allowedContentTypes: [.pdf],
allowsMultipleSelection: false
) { result in
do {
guard let fileURL: URL = try result.get().first else { return print("error fileURL") }
fileURL.startAccessingSecurityScopedResource()
guard let pdf = PDFDocument.init(url: fileURL) else {return print("error pdf")}
let pageCount = 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)
}
fileURL.stopAccessingSecurityScopedResource()
print(documentContent.string)
} catch {
print(error)
}
}
我有一个非常简单的应用程序,它有一个按钮可以打开文件应用程序并在控制台中打印 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
,但我找不到任何东西可以让它在真实设备上工作。
在此先感谢您的支持!
找到答案了!
将 if let
更改为 guard let
添加了 fileURL.startAccessingSecurityScopedResource()
和 fileURL.stopAccessingSecurityScopedResource()
Button(action:{
openFile.toggle()
}){
Image(systemName: "exclamationmark.circle.fill")
}
}.fileImporter(
isPresented: $openFile,
allowedContentTypes: [.pdf],
allowsMultipleSelection: false
) { result in
do {
guard let fileURL: URL = try result.get().first else { return print("error fileURL") }
fileURL.startAccessingSecurityScopedResource()
guard let pdf = PDFDocument.init(url: fileURL) else {return print("error pdf")}
let pageCount = 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)
}
fileURL.stopAccessingSecurityScopedResource()
print(documentContent.string)
} catch {
print(error)
}
}