【问题标题】:Dismissing WKWebView when navigating to a specific URL导航到特定 URL 时关闭 WKWebView
【发布时间】:2021-01-30 19:09:00
【问题描述】:

我目前正在使用 WKWebView 加载 HTML 文件而不是 URL,并且我正在寻找一种在用户导航到特定 URL 时关闭工作表的方法。

struct WebView: UIViewRepresentable {
  @Binding var text: String
   
  func makeUIView(context: Context) -> WKWebView {
    return WKWebView()
  }
   
  func updateUIView(_ uiView: WKWebView, context: Context) {
    uiView.loadHTMLString(text, baseURL: nil)
  }
}

我正在使用这个 UIViewRepresentable 来加载 HTML 字符串,并且我正在使用这个表单来显示 WebView:

.sheet(isPresented: $isSheetPresented, onDismiss: {
            self.checkthreed()
        }, content: {
            WebView(text: $decodedString) 
        })

用户可以导航到 2 个 URL:

  • example.com/failure
  • example.com/success

当用户导航到这些 URL 中的任何一个时,我如何关闭工作表?

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    这是一个非常基本的非常,您可以根据自己的需要进行调整:

    struct WebView: UIViewRepresentable {
        @Binding var text: String
        var closeFunction : (() -> Void)?
       
        class Coordinator : NSObject, WKNavigationDelegate {
            var closeFunction : (() -> Void)?
            
            func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
                if let urlStr = navigationAction.request.url?.absoluteString {
                    if urlStr == "test" {
                        closeFunction?()
                    }
                }
                decisionHandler(.allow)
            }
        }
        
        func makeCoordinator() -> Coordinator {
            return Coordinator()
        }
        
      func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
      }
       
      func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.navigationDelegate = context.coordinator
        context.coordinator.closeFunction = closeFunction
        uiView.loadHTMLString(text, baseURL: nil)
      }
    }
    
    
    struct ContentView: View {
        @State var sheetPresented = true
        
        var body: some View {
            Text("Hi")
                .sheet(isPresented: $sheetPresented, content: {
                    WebView(text: .constant("<a href=\"test\">Test link</a><br><a href=\"test2\">Test 2</a>"),
                            closeFunction: {
                                sheetPresented = false
                            })
                })
            }
    }
    

    WKWebView 附加了一个WKNavigationDelegate,它可以接收有关正在加载的 URL 的通知。您可以在我的示例中看到“test”会触发关闭,而“test2”则不会。

    WKNavigationDelegateUIViewRepresentableCoordinator 的一部分。请注意,我将closeFunction 设置为可选闭包,因此您必须确保设置它,否则不会发生任何事情。另一种方法是传递Binding&lt;Bool&gt; 以获取正在呈现的工作表并直接对其进行操作。

    【讨论】:

    • 感谢您的评论!这个解决方案很可能会解决我的问题,但我不知道如何设置closeFunction。我该怎么做?
    • 我在 WebView 上使用它作为尾随闭包,这不是很清楚。我已经更新了我的示例,因此当您在 ContentView 中创建 WebView 时,您可以看到它是一个命名参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多