I am facing an issue with the webview_flutter package in my Flutter app. On Android, everything works fine. When I upload an item and try to delete it, a pop-up window appears asking for confirmation before proceeding with the deletion. However, on iOS, the pop-up window does not appear when I click on the delete button.
I have searched extensively for a solution but haven't found any relevant information or examples. I have already implemented the webview_flutter package correctly, and other functionalities are working as expected on both Android and iOS.
You will need to implement an extension to FWFUIDelegate, add this implement to AppDelegate file, and remember to import the module webview_flutter_wkwebview.
This implement will help you open alertPanel and confirmPanel for iOS webview_flutter.
Here is the implementation:
import webview_flutter_wkwebview
@objc //This annotation allow Swift can be reachable by Objective-C
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!
}
}