0

I am loading PDF file in PDFView but unable to get PDF File URL from Bundle.

Please check below code :

let pdfView = PDFView(frame: self.view.bounds)
let myFileName = "sample"
guard let url = Bundle.main.url(forResource: myFileName, withExtension: "pdf") else {
         return
}
pdfView.document = PDFDocument(url: url)

self.view.addSubview(pdfView)

Every time I am getting nil in url.

Edit :

Here is my Project Window:

enter image description here

Am I making any silly mistake ?

Please guide me.

Thanks in Advance.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80

2 Answers2

5

I got problem not sure it's in Latest XCode but in XCode 9.5(Beta) while you adding any external file in your project and select bundle target still its not adding into bundle.

Solution :

You need to click on file which you want to add into your target bundle and check manually from property window.

Please check below screen shot from more detail:

While adding file into project I have checked it but still its not adding in selected target.

enter image description here

Now what I need only check related target and its working as expected.

enter image description here

Not sure its a bug or new update in Latest XCode.

But my problem is solved.

Thanks.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
1

No, you're not making a silly mistake.

I believe you simply need to add your pdf file as a resource to be copied.

Go into your Project's Build Phases and click on "Copy Bundle Resources" and then add "sample.pdf" to the list of files being copied, like this:

Add File to list of files to be copied

And then make sure it appears in that list, like this:

Sample.pdf appears here

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215