Is it possible to load local pdf file on SFSafariViewController?

ぐ巨炮叔叔 提交于 2019-12-05 23:15:27

It is not possible to show local pdf in SFSafariViewController, but it is possible to display it in a WKWebView. SFSafariViewController only support http and https schemes.

From SFSafariViewController documentation:

If your app lets users view websites from anywhere on the Internet, use the SFSafariViewController class. If your app customizes, interacts with, or controls the display of web content, use the WKWebView class.

From SFSafariViewController init(url:) documentation:

The URL must use the http or https scheme.

To show local pdf file in WKWebView, you can do something similar to the following code:

    let mainBundle:Bundle = Bundle(for: WKWebContainerView.self)
    let path = mainBundle.path(forResource: "sample", ofType: "pdf")
    let pdfUrl = URL(fileURLWithPath: path!)
    let folderUrl = URL(fileURLWithPath: mainBundle.bundlePath, isDirectory: true)
    webView.loadFileURL(pdfUrl, allowingReadAccessTo: folderUrl)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!