How do I correctly show a PDF document?

How do I correctly show a PDF document?

iPad and Xcode 15.4

Within my GameViewController, I have:

func presentScene(_ theScene: SKScene) {
        
    theScene.scaleMode = .resizeFill

    if let skView = self.view as? SKView {
        skView.ignoresSiblingOrder = true
        skView.showsFPS = true
        skView.showsNodeCount = true
            
#if os(iOS)
        let theTransition = SKTransition.doorway(withDuration: 2.0)
        skView.presentScene(theScene, transition: theTransition)
#elseif os(tvOS)
        skView.presentScene(theScene)
#endif
    }

}   // presentScene

I believe presentScene(theScene) goes to the sceneDidLoad() func of theScene which adds various SKSpriteNodes to the scene via a call to addChild(theNode).

So far so good ...

Until I have a SKScene wherein I wish to display a PDF.

I use this snippet to display this PDF with this call within the SKScene's sceneDisLoad():

displayPDF("ABOUT_OLD_WEST_LOCOMOTIVE")

func displayPDF(_ itsName:String) {
    
    let pdfView = PDFView()

    guard let path = Bundle.main.path(forResource: itsName, ofType: "pdf")
    else { return }
        
    print("path")

    guard let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path))
    else { return }
            
    print("pdfDocument")
            
    pdfView.displayMode = .singlePageContinuous
    pdfView.autoScales = true
    pdfView.displayDirection = .vertical
    pdfView.document = pdfDocument

}   // displayPDF

The 2 print statements show, yet the SKScene does not display the PDF via pdfView.document = pdfDocument?

Anyone have a clue what errors I have committed?

Appreciate it.

Answered by JohnLove in 797812022

SUCCESS!!

Somehow my pdf disappeared from my Copy Bundle Resources. Clicked + to add it back in. Lordy, I cannot possibly thank all of you enough for your extraordinary patience.

Starting over with the suggestions offered so far,

I have this in AppDelegate:

var pdfView: PDFView!

In my GameViewController’s viewDidLoad(), I call:

`addPDFView()’

which looks like this:

func addPDFView() {
        
    pdfView = PDFView(frame: self.view.frame)
        
    pdfView.displayMode = .singlePageContinuous
    pdfView.autoScales = true
    pdfView.displayDirection = .vertical

    view.addSubview(pdfView)
        
}   // addPDFView

Then, in my SKScene’s sceneDidLoad(), I call:

displayPDF("ABOUT_OLD_WEST_LOCOMOTIVE")

which looks like this:

func displayPDF(_ itsName:String) {
        
    guard (thisSceneName == "AboutIOSScene") else { return }
        
    let pdfView = PDFView()   // see GameViewController's addPDFView()

    guard let path = Bundle.main.path(forResource: itsName, ofType: "pdf")
    else { return }
            
    print("path =", path)

    guard let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path))
    else { return }
                
    print("pdfDocument =", pdfDocument)
                
    pdfView.document = pdfDocument
        
}   // displayPDF

Once again, the 2 print statements are shown .. but no pdfView.document

John

Please note that the only reason I do not place all the PDFView code in my GameViewController is because I have 3 SKScenes, only one of which uses a PDFDocument. As a result, I add the PDFView to all SKScenes within my GameViewController and then fill it in the one SKScene that will show the PDFDocument.

As long as I am asking for help, how do I successfully place my pdfView on top Being a UIView, it has no .zPosition?

I'm getting there ...

Again, I call addPDFView() + displayPDF("ABOUT_OLD_WEST_LOCOMOTIVE.pdf"

within my specific SKScene's sceneDidLoad() because I have multiple SKScenes.

and an empty PDFView shows ... which tells me my Project is not seeing my .pdf file.

I do not understand this because when I select my Target's Build Phases and expand Copy Bundle Resources, the above .pdf file is listed.

???

Accepted Answer

SUCCESS!!

Somehow my pdf disappeared from my Copy Bundle Resources. Clicked + to add it back in. Lordy, I cannot possibly thank all of you enough for your extraordinary patience.

How do I correctly show a PDF document?
 
 
Q