Is it possible to load local pdf file on SFSafariV

2019-05-16 11:28发布

I tried with WKWebViewController, but still not working.

url = [[NSBundle mainBundle] URLForResource:@"manual_eos_1d_x" withExtension:@"pdf"];

WKWebViewController * controllerweb = (WKWebViewController *)[segue destinationViewController];

controllerweb.url = url;

1条回答
Root(大扎)
2楼-- · 2019-05-16 11:43

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)
查看更多
登录 后发表回答