在 WKUIDelegate SwiftUI 上实现 Javascript 警报并确认?

Ohs*_*sik 8 javascript swift swiftui

由于我是 Swift 新手,我不确定如何为 Swift 编写一个函数,以从 Web 应用程序中进行交互式 Javascript 警报和确认。我正在使用 SwiftUI 创建一个 Web 应用程序,需要为我的 Swift Web 应用程序实现一个功能,以从 Web 应用程序正确显示 Javascript 警报和确认。

这是我到目前为止的代码。

import SwiftUI
import WebKit

struct Webview : UIViewRepresentable {
    let request: URLRequest
    var webview: WKWebView?

    init(web: WKWebView?, req: URLRequest) {
        self.webview = WKWebView()
        self.request = req
    }

    class Coordinator: NSObject, WKUIDelegate {
        var parent: Webview

        init(_ parent: Webview) {
            self.parent = parent
        }

        // Delegate methods go here

        func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
            // alert functionality goes here

        }
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: Context) -> WKWebView  {
        return webview!
    }

    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.uiDelegate = context.coordinator
        uiView.load(request)
    }

    func goBack(){
        webview?.goBack()
    }

    func goForward(){
        webview?.goForward()
    }

    func reload(){
        webview?.reload()
    }
}

struct ContentView: View {

    let webview = Webview(web: nil, req: URLRequest(url: URL(string: "https://google.com")!))

    var body: some View {
        VStack {
            webview
            HStack() {
                Button(action: {
                    self.webview.goBack()
                }){
                    Image(systemName: "chevron.left")
                }.padding(32)

                Button(action: {
                    self.webview.reload()
                }){
                    Image(systemName: "arrow.clockwise")
                }.padding(32)

                Button(action: {
                    self.webview.goForward()
                }){
                    Image(systemName: "chevron.right")
                }.padding(32)
            }.frame(height: 40)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Run Code Online (Sandbox Code Playgroud)

如何添加一个函数来显示AlertConfirm正确地添加到上面的现有代码中?

fuz*_*uzz 0

如果您想坚持并使用来自(例如)UIViewRepresentable的警报而不是 SwiftUI\xe2\x80\x99s 警报:UIKitUIAlertController

\n
    \n
  1. 在您的结构中创建一个函数Webview来显示UIKit警报:

    \n
    func showAlert(_ message: String) {\n   let alertController = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)\n   alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))\n\n   // You need to access the UIWindow of the app to present the alert\n   if let viewController = UIApplication.shared.windows.first?.rootViewController {\n      viewController.present(alertController, animated: true, completion: nil)\n   }\n}\n
    Run Code Online (Sandbox Code Playgroud)\n
  2. \n
  3. 修改您的 JavaScript 函数以在显示警报时调用此 Swift 函数:

    \n
    function showAlert(message) {\n   window.webkit.messageHandlers.alertHandler.postMessage(message);\n}\n
    Run Code Online (Sandbox Code Playgroud)\n
  4. \n
  5. 在该方法中,当您收到来自 JavaScript 的消息时userContentController(_:didReceive:)调用该函数:showAlert

    \n
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {\n   if message.name == "alertHandler", let messageBody = message.body as? String {\n      showAlert(messageBody)\n   }\n}\n
    Run Code Online (Sandbox Code Playgroud)\n
  6. \n
\n

function showAlert(message)当从网页调用JavaScript 时,它将触发showAlertWebview 结构中的函数,该函数将UIKit使用UIAlertController.

\n