我正在使用 WkWebview 加载一个网站,该网站包含一些 javascript 操作。对于 iPhone,它工作正常,因为在用户选择面板中的可用操作之前,confirmPanel 不允许用户触摸面板外部。但是,对于 iPad,它的功能与 iPhone 不同。每当 我尝试触摸警报面板以外的地方,它会崩溃。请查看代码下方的崩溃日志。我尝试使用 UITapGestureRecognizer,但它仍然无法正常工作。
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
completionHandler(true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
completionHandler(false)
}))
if UIDevice.current.userInterfaceIdiom != .phone {
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
}
self.present(alertController, animated: true, completion: nil)
}
错误信息,
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[webViewios.MainVC webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:] was not called' *** First throw call stack: (0x21a2f91b 0x211cae17 0x21a2f861 0x287d7293 0x288111b9 0x2886db69 0x21603ac3 0xccd4c 0x8d9e1f 0xcc6d8 0x8d9e1f 0xd1718 0x21603ac3 0x26400cd1 0x263ff26b 0x211e53a9 0x2193ef89 0x219f006f 0x2193f229 0x2193f015 0x22f2fac9 0x26013189 0xd8824 0x215e7873) libc++abi.dylib: terminating with uncaught exception of type NSException
最佳答案
在 iPad 中,当您呈现操作表时,取消操作会自动映射到操作表控制面板外的点击。您需要更改显示 UIAlertController
的 Cancel
按钮的方式。
iPad 上没有取消按钮的更新代码
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
completionHandler(true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) in
completionHandler(false)
}))
if UIDevice.current.userInterfaceIdiom != .phone {
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
}
self.present(alertController, animated: true, completion: nil)
}
在 iPad 上使用取消按钮编写代码
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
completionHandler(true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
completionHandler(false)
}))
if UIDevice.current.userInterfaceIdiom != .phone {
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) in
completionHandler(false)
}))
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
}
self.present(alertController, animated: true, completion: nil)
}
关于ios - 如何在 WKWebView 中关闭 ipad 的 alertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49377017/