【问题标题】:Swift - Getting PDF data working in Simulator but not on a real iPhoneSwift - 让 PDF 数据在模拟器中工作,但不能在真正的 iPhone 上工作
【发布时间】: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 placeApplication supports iTunes file sharing,但我只是找不到任何东西让它在真实设备中工作。

提前感谢您的支持!

【问题讨论】:

  • 嘿 LeonardoXUI,你能打印出你的文件 URL 吗?那里可能有问题。
  • 嗨,乔希,fileURL 打印正确,所以问题不存在。我可以确认问题从if let pdf = PDFDocument(url: fileURL) 行开始。仍然不知道是什么导致它不起作用。它适用于模拟器。
  • 我认为您的 PDFDocument 初始化可能无法生成正确的 pdf 对象。您可以将 PDFDocument 初始化代码添加到您的问题中吗?
  • 我目前没有初始化程序,因为我遵循的教程没有使用...

标签: ios swift xcode pdfkit


【解决方案1】:

找到答案了!

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

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多