首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我不能在网页视图中打开App链接?

为什么我不能在网页视图中打开App链接?
EN

Stack Overflow用户
提问于 2022-01-26 00:56:29
回答 1查看 1.3K关注 0票数 4

我总是犯这样的错误:

代码语言:javascript
运行
复制
WebPageProxy::didFailProvisionalLoadForFrame: frameID=3, domain=WebKitErrorDomain, code=102

正常链接正在工作,但AppStore链接不工作。

我想要的是打开AppStore的链接--我不能在本地这样做,因为网络是从Qualtrics加载的。

我尝试添加navigationAction函数,但这不起作用,我猜可能请求需要一些时间,我需要一种异步加载数据的方法,但老实说,我真的不知道

代码语言:javascript
运行
复制
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()
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-26 05:23:33

一些关于点击它们的链接可能会激活操作/用非HTTPs的url方案重定向,如

  • 打开一个新选项卡的_blank
  • 启动邮件应用程序的mailto
  • 设备OSs熟悉的其他一些深层链接技术

我相信应用程序商店链接使用了上述的组合,而WKWebView不能处理非HTTPs方案。

您可以做的是使用WKNavigationDelegate侦听失败的URL并相应地处理它们。

我不使用SwiftUI,但我认为你可以得到图片。

使用与两个链接相同的HTML设置

代码语言:javascript
运行
复制
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函数

  1. decidePolicyFor navigationAction (文件链接)允许处理即使不遵循HTTPs方案的urls
  2. 导航失败委托函数 webView didFailProvisionalNavigation并检查iOS是否能够处理新选项卡、邮件、深度链接等打开的内容,因此在您的情况下,它将打开应用程序商店。
  3. 您还可以在此WKNavigationDelegate函数中实现与第2点相同的逻辑,以防万一
代码语言:javascript
运行
复制
// 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)
            }
        }
    }
}

试一试,看看这是否解决了你的问题。就我而言,这两个链接似乎运作良好:

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70857547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档