ios - 如何在 WKWebView 中关闭 ipad 的 alertController

标签 ios swift wkwebview uialertcontroller

我正在使用 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 中,当您呈现操作表时,取消操作会自动映射到操作表控制面板外的点击。您需要更改显示 UIAlertControllerCancel 按钮的方式。

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/

相关文章:

ios - Firebase 授权谷歌 + Facebook = 崩溃

ios - Swift:自动布局约束问题。人像与风景

ios - 如何在应用程序中设置本地化方向?(RTL 如果用户选择阿拉伯语,LTR 则选择语言为英语)

ios - 在 WKWebView 中填充输入文本字段

wkwebview - 如何将 WKWebView 恢复到应用程序关闭前的状态?

ios - 警告 : Attempt to present <UIAlertController:> on <App name:> whose view is not in the window hierarchy

ios - 在 iOS 中获取展开/折叠 UITableView 的动态 UIWebview 内容高度

ios - 有没有办法确定 WKWebView 或 SFSafariViewController View 中某个点的元素是什么?

ios - WKWebView 和键盘外观 LayoutConstraints 错误的 Swift 问题

ios - 选择按钮时如何连接数组