Search code examples
iosswiftxcodepdfkit

Swift - Getting PDF data working in Simulator but not on a real iPhone


I have a really simple app with a button that opens the files app and prints the content of a PDF in the console. This perfectly works on the simulator, but when using the app in a real iOS device, the app won't print anything.


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

In the Info.plist I already added the Keys Supports opening documents in place and Application supports iTunes file sharing, but I just can't find anything to make it work in a real device.

Thanks in advance for your support!


Solution

  • Found the answer!

    Changed the if let to guard let Added fileURL.startAccessingSecurityScopedResource() and 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)
                }
            }