我总是犯这样的错误:
WebPageProxy::didFailProvisionalLoadForFrame: frameID=3, domain=WebKitErrorDomain, code=102
正常链接正在工作,但AppStore链接不工作。
我想要的是打开AppStore的链接--我不能在本地这样做,因为网络是从Qualtrics加载的。
我尝试添加navigationAction函数,但这不起作用,我猜可能请求需要一些时间,我需要一种异步加载数据的方法,但老实说,我真的不知道
import SwiftUI
import WebKit
struct WebView: UIViewRepresentable {
let html = """
<a href="https://apps.apple.com/us/app/directorio-notarios-cdmx/id1544000342"> Appstore link dont open</a></span></span><br />
<a href="https://landercorp.mx" rel="noopener"> Normal link </a></span></span><br />
"""
var loadStatusChanged: ((Bool, Error?) -> Void)? = nil
func makeCoordinator() -> WebView.Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> WKWebView {
let view = WKWebView()
view.navigationDelegate = context.coordinator
view.loadHTMLString(html, baseURL: nil)
return view
}
func updateUIView(_ uiView: WKWebView, context: Context) {
}
class Coordinator: NSObject, WKNavigationDelegate {
let parent: WebView
init(_ parent: WebView) {
self.parent = parent
}
}
}
struct ContentView: View {
var body: some View {
WebView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
发布于 2022-01-26 05:23:33
一些关于点击它们的链接可能会激活操作/用非HTTPs的url方案重定向,如
_blank
mailto
我相信应用程序商店链接使用了上述的组合,而WKWebView不能处理非HTTPs方案。
您可以做的是使用WKNavigationDelegate
侦听失败的URL并相应地处理它们。
我不使用SwiftUI,但我认为你可以得到图片。
使用与两个链接相同的HTML设置
class ViewController: UIViewController, WKNavigationDelegate
{
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
let html = """
<a href="https://apps.apple.com/us/app/directorio-notarios-cdmx/id1544000342"> Appstore link dont open</a></span></span><br />
<a href="https://landercorp.mx" rel="noopener"> Normal link </a></span></span><br />
"""
let webview = WKWebView()
webview.frame = view.bounds
webview.navigationDelegate = self
view.addSubview(webview)
webview.loadHTMLString(html, baseURL: nil)
}
}
然后我实现了这些WKNavigationDelegate
函数
decidePolicyFor navigationAction
(文件链接)允许处理即使不遵循HTTPs方案的urlswebView didFailProvisionalNavigation
并检查iOS是否能够处理新选项卡、邮件、深度链接等打开的内容,因此在您的情况下,它将打开应用程序商店。// MARK: WKNavigationDelegates
func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
{
decisionHandler(.allow)
}
func webView(_ webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
withError error: Error)
{
manageFailedNavigation(webView,
didFail: navigation,
withError: error)
}
func webView(_ webView: WKWebView,
didFail navigation: WKNavigation!,
withError error: Error)
{
manageFailedNavigation(webView,
didFail: navigation,
withError: error)
}
private func manageFailedNavigation(_ webView: WKWebView,
didFail navigation: WKNavigation!,
withError error: Error)
{
// Check if this failed because of mailto, _blank, deep links etc
// I have commented out how to check for a specific case like open in a new tab,
// you can try to handle each case as you wish
if error.localizedDescription
== "Redirection to URL with a scheme that is not HTTP(S)"
//let url = webView.url, url.description.lowercased().range(of: "blank") != nil
{
// Convert error to NSError so we can access the url
let nsError = error as NSError
// Get the url from the error
// This key could change in future iOS releases
if let failedURL = nsError.userInfo["NSErrorFailingURLKey"] as? URL
{
// Check if the action can be handled by iOS
if UIApplication.shared.canOpenURL(failedURL)
{
// Request iOS to open handle the link
UIApplication.shared.open(failedURL, options: [:],
completionHandler: nil)
}
}
}
}
试一试,看看这是否解决了你的问题。就我而言,这两个链接似乎运作良好:
https://stackoverflow.com/questions/70857547
复制相似问题