弹出确认在 iOS 的 webview_flutter 中不起作用

问题描述 投票:0回答:1

我在 Flutter 应用程序中遇到了 webview_flutter 包的问题。在 Android 上,一切正常。当我上传项目并尝试删除它时,会出现一个弹出窗口,要求在继续删除之前进行确认。但是,在iOS上,当我点击删除按钮时,不会出现弹出窗口。

我广泛搜索了解决方案,但没有找到任何相关信息或示例。我已经正确实现了 webview_flutter 包,并且其他功能在 Android 和 iOS 上都按预期工作。

flutter webview flutter-dependencies
1个回答
0
投票

您需要实现FWFUIDelegate的扩展,将此实现添加到AppDelegate文件中,并记住导入模块webview_flutter_wkwebview

这是实现:

import webview_flutter_wkwebview

@objc
extension FWFUIDelegate {
    @objc
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
        let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default) { _ in
            completionHandler()
        })
        topViewController()?.present(alert, animated: true, completion: nil)
    }
    
    @objc
    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
        let alert = UIAlertController(title: "", message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default) { _ in
            completionHandler(true)
        })
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
            completionHandler(false)
        })
        topViewController()?.present(alert, animated: true, completion: nil)
    }
    
    func topViewController() -> UIViewController? {
        return topViewControllerWithRootViewController(getCurrentWindow().rootViewController)
    }
    
    func topViewControllerWithRootViewController(_ viewController: UIViewController?) -> UIViewController? {
        guard let viewController = viewController else { return nil }
        if let presentedViewController = viewController.presentedViewController {
            return topViewControllerWithRootViewController(presentedViewController)
        } else if let tabBarController = viewController as? UITabBarController {
            return topViewControllerWithRootViewController(tabBarController.selectedViewController)
        } else if let navigationController = viewController as? UINavigationController {
            return topViewControllerWithRootViewController(navigationController.visibleViewController)
        } else {
            return viewController
        }
    }
    
    func getCurrentWindow() -> UIWindow {
        var window = UIApplication.shared.keyWindow
        if window?.windowLevel != .normal {
            for window in UIApplication.shared.windows {
                if window.windowLevel == .normal {
                    return window
                }
            }
        }
        return window!
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.