Not getting file URL from bundle

邮差的信 提交于 2019-12-12 04:57:44

问题


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:

Am I making any silly mistake ?

Please guide me.

Thanks in Advance.


回答1:


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.

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

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

But my problem is solved.

Thanks.




回答2:


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:

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



来源:https://stackoverflow.com/questions/45749639/not-getting-file-url-from-bundle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!