gpt4 book ai didi

javascript - WKWebView:应用程序崩溃并出现 'NSInternalInconsistencyException' 异常,因为 'Completion handler passed to X was not called'

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:39 30 4
gpt4 key购买 nike

当尝试在 iOS 上的 WKWebView 中同时显示两个 JavaScript 警告框时,会发生以下异常。应用因此崩溃。

2017-12-23 00:46:29.434 Couch[3192:3428809] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[XViewController webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:] was not called'

这是有问题的 WKWebView 委托(delegate)函数:

func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo,
completionHandler: @escaping () -> Void) {

let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
completionHandler()
}))

if let presenter = alertController.popoverPresentationController {
presenter.sourceView = self.view
presenter.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
}
if presentedViewController == nil {
present(alertController, animated: true, completion: nil)
}
}

最佳答案

在呈现第一个 UIAlertController 之后,presentedViewController 不再是 nil。然后您的委托(delegate)方法不会显示第二个/下一个 UIAlertController 并静默返回。然而,您仍然需要调用 completionHandler()

if presentedViewController == nil
{
// You get here for the second alert box, which is not shown.
present(alertController, animated: true, completion: nil)
}
else
{
completionHandler()
}

第二个、第三个……UIAlertController 从未出现。如果这不是您想要的,请查看 here例如。

关于javascript - WKWebView:应用程序崩溃并出现 'NSInternalInconsistencyException' 异常,因为 'Completion handler passed to X was not called',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47951132/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com