WKWebView中如何实现"Are you sure you want to submit the form again"
How to implement "Are you sure you want to submit the form again" in WKWebView
在 Safari 中,尝试重新加载表单时会显示警告,而 WKWebView 在重新加载时不会显示警告。
如何实现 "Are you sure you want to submit the form again"?
您可以通过创建 UIAlertController 轻松添加警报,然后向其添加自定义操作。
以您的情况为例:
//Create the alert using UIAlertController. Add your custom title and custom message
let alert = UIAlertController(title: "Information", message: "Are you sure you want to submit the form again", preferredStyle: .alert)
//Add custom buttons with different style
alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
//Present the alertView
self.presentViewController(alert, animated: true, completion: nil)
你可以把它做成一个函数,然后你可以在刷新页面时调用它。
实施WKNavigationDelegate#webView(_:decidePolicyFor:decisionHandler:)。
然后,如果 navigationAction.navigationType 是 formResubmitted,显示警报。
对于警报的提交或取消按钮操作,分别调用 decisionHandler(.allow) 或 decisionHandler(.cancel)。
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if case .formResubmitted = navigationAction.navigationType {
// Show alert
}
}
在 Safari 中,尝试重新加载表单时会显示警告,而 WKWebView 在重新加载时不会显示警告。
如何实现 "Are you sure you want to submit the form again"?
您可以通过创建 UIAlertController 轻松添加警报,然后向其添加自定义操作。
以您的情况为例:
//Create the alert using UIAlertController. Add your custom title and custom message
let alert = UIAlertController(title: "Information", message: "Are you sure you want to submit the form again", preferredStyle: .alert)
//Add custom buttons with different style
alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
//Present the alertView
self.presentViewController(alert, animated: true, completion: nil)
你可以把它做成一个函数,然后你可以在刷新页面时调用它。
实施WKNavigationDelegate#webView(_:decidePolicyFor:decisionHandler:)。
然后,如果 navigationAction.navigationType 是 formResubmitted,显示警报。
对于警报的提交或取消按钮操作,分别调用 decisionHandler(.allow) 或 decisionHandler(.cancel)。
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if case .formResubmitted = navigationAction.navigationType {
// Show alert
}
}