Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 什么会导致永远不会调用UIAlertController操作?_Ios_Uialertcontroller_Wkwebview - Fatal编程技术网

Ios 什么会导致永远不会调用UIAlertController操作?

Ios 什么会导致永远不会调用UIAlertController操作?,ios,uialertcontroller,wkwebview,Ios,Uialertcontroller,Wkwebview,在崩溃报告中,我看到nsinternalinconsistenceexception从WKWebView委托方法中抛出,用于提示JavaScript警报,即“未调用传递给-[MyClass webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:]的完成处理程序” 每个UIAlertAction都从其处理程序调用WKWebView完成处理程序。唯一的解释是,警报将在不调用任何操作的情况下取消UIA

在崩溃报告中,我看到
nsinternalinconsistenceexception
WKWebView
委托方法中抛出,用于提示JavaScript警报,即“未调用传递给-[MyClass webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:]的完成处理程序”

每个
UIAlertAction
都从其处理程序调用
WKWebView
完成处理程序。唯一的解释是,警报将在不调用任何操作的情况下取消
UIAlertView
为此类情况提供了委托方法,但
UIAlertController
不提供该级别的控制

有人想出了解决这个问题的办法吗

我考虑过使用Apple在
CompletionHandlerCallChecker
(WebKit)中使用的相同技术来捕获我自己的回调失败,并调用WKWebView的处理程序以防止虚假异常。看起来非常笨拙,而且我还不确定它是否有效。我宁愿一开始就阻止这种事情发生

编辑:我知道以编程方式解除警报控制器会产生这种行为,这是不幸的,但我不知道iOS决定在没有用户交互的情况下解除控制器的情况

编辑2:对于那些请求代码的人来说,这实际上是最低限度的实现:

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
  [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    completionHandler();
  }]];
  [self presentViewController:alertController animated:YES completion:nil];
}

以下是我的拙劣但有效的解决方案:

class CompletionHandlerCallCheckerDefeater: NSObject {
  private var calledCompletionHandler: Bool = false
  private var fallbackHandler: () -> Void

  init(fallbackHandler: () -> Void) {
    self.fallbackHandler = fallbackHandler
  }

  deinit {
    if (!calledCompletionHandler) {
      fallbackHandler()
    }
  }

  func didCallCompletionHandler() {
    calledCompletionHandler = true
  }
}
在委托回调中,如下所示使用它:

let defeater = CompletionHandlerCallCheckerDefeater(fallbackHandler: completionHandler)
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title:"OK", style: .Default) { (action) in
    completionHandler()
    defeater.didCallCompletionHandler()
})
presentViewController(alertController, animated: true, completion:  nil)

为fallbackHandler分配任何必要的内容,它不必是传入的块,只需要使用适当的参数调用completionHandler。当所有引用被释放时,即当警报被销毁时,“失败者”将被销毁,如果从未调用didCallCompletionHandler,它将调用fallbackHandler(调用completionHandler),因此避免了例外情况。

在问题中发布相关代码。您是否有取消按钮的UIAlertAction?您是否找到了解决方案?如果没有,您将如何执行您提到的CompletionHandlerCallChecker?我有完全相同的问题,一些其他用户界面可能会出现,导致警报被解除,而不是被告知只会导致此异常。