我正在使用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)
}
错误讯息
由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'完成处理程序传递给 - [webViewios.MainVC webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:]未被调用' ***第一次抛出调用堆栈: (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:以NSException类型的未捕获异常终止
答案 0 :(得分:0)
如果UIAlertController
UIViewController
可见,则显示您的WKWebView
。如果不可见,请直接执行completionHandler
并返回。
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 = []
}
}
if self.viewIfLoaded?.window != nil {
self.present(alertController, animated: true, completion: nil)
}
else {
completionHandler()
}
}
答案 1 :(得分:0)
在iPad中,当您显示操作表时,取消操作会自动映射到操作表控制面板外的水龙头。您需要更改Cancel
UIAlertController
按钮的显示方式。
在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)
}