WkWebView 不加载指向 pdf 的链接 [英] WkWebView does not load links to pdfs
问题描述
WKWebView 未加载链接.我将用户链接到隐私政策页面,该页面有一组链接.这些链接都是由 wix 托管的所有 pdf.在 safari 和 Chrome 上它有效,但在 WKWebView 上无效.当页面加载时,当您单击链接时,我只是收到一个错误:
The WKWebView is not loading links. I am linking users to a privacy policy page, and the page has a group of links. The links are all pdfs hosted by wix. On safari and Chrome it works, but not on WKWebView. When the page loads, and you click the links, I just get an error:
Unknown result for URL 0x28157d110 (https)
这就是我加载网络视图的方式...
This is how I'm loading the web view...
webView.load(URLRequest(url: URL(string: "https://mywebsite.io/legal")!))
这是与其他问题不同,因为我无意下载pdf- 我只想以与 Safari 相同的方式显示它.
This is different from other questions because I have no intention of downloading the pdf - I just want to display it the same way that Safari does.
我刚刚用 UIWebView(已弃用)和 pdfs 加载替换了 WKWebView.问题在于 WKWebView.Pdfs 是支持 ssl 的 https ->
I just replaced WKWebView with UIWebView (deprecated) and the pdfs load. The issue is with WKWebView. The Pdfs are ssl-enabled https ->
let req = URLRequest(url: URL(string: "https://mywebsite.io/legal")!)
legacyWebView.loadRequest(req)
这里有一个页面如何使用 wkwebview 打开指向 PDF 的链接,这表明您必须在打开 pdf 之前知道链接 URL,但我认为这不是真的.
There is a page here How to open a Link to a PDF with wkwebview that suggests that you must know the link URL before opening the pdf, I don't think this is true though.
我实现了 2 个委托方法,包括@Kiril 在下面建议的方法.指向 pdf 的链接仍未打开.
I have 2 delegate methods implemented, including the one suggested below by @Kiril. Links to pdfs still do not open.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(WKNavigationActionPolicy.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
decisionHandler(WKNavigationResponsePolicy.allow)
}
推荐答案
您可以检查以下几点:
验证
链接不包含
target="_blank"
属性,因为 WKWebView 不知道如何在一个新标签.请参阅 https://stackoverflow.com/a/25853806/810466 了解如何解决此问题.
Verify that the
<a href
links doesn't containtarget="_blank"
attribute since WKWebView doesn't know how to open the link in a new tab. see https://stackoverflow.com/a/25853806/810466 for how to work around that.
检查链接是否为 HTTPS 或使用 Allow Arbitrary Loads
选项更新 App Transport Security Settings
选项
Check if the link is HTTPS or update the App Transport Security Settings
with the Allow Arbitrary Loads
option
确保仅在将 WKWebView 添加到 didMoveToParentViewController:
中的视图层次结构后才开始加载请求,因为如果它试图在视图层次结构之外运行,它可能会使 javascript 失败
Make sure that you start the loading request only after adding the WKWebView to the view hierarchy in didMoveToParentViewController:
since it may make javascript to fail if it tries to run outside the view hierarchy
实现 WKWebView NavigationDelegate 方法并确保在决定请求策略时返回 WKNavigationActionPolicyAllow
Implement the WKWebView NavigationDelegate methods and make sure you return WKNavigationActionPolicyAllow
when deciding the policy for the request
这篇关于WkWebView 不加载指向 pdf 的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!