What can cause UIAlertController actions to never be called?

Viewed 1098

In crash reports I'm seeing NSInternalInconsistencyException get thrown from the WKWebView delegate methods for prompting JavaScript alerts, ie. "Completion handler passed to -[MyClass webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:] was not called".

Every UIAlertAction calls the WKWebView completion handler from its handler. The only explanation is that the alert is being canceled without invoking any action. UIAlertView had delegate methods for cases like this, but UIAlertController does not offer that level of control.

Has anyone devised a solution for this?

I've considered using the same technique Apple uses in CompletionHandlerCallChecker (in WebKit) to capture my own callback's failure to be invoked, and invoke the WKWebView's handler to prevent the spurious exception. Seems awfully kludgy, and I'm not yet sure it would work. I'd rather prevent this from happening in the first place.

Edit: I know that programmatically dismissing the alert controller produces this behavior, which is unfortunate, but I do not know the condition where iOS decides to dismiss the controller without user interaction.

Edit 2: For those requesting the code, it is really the minimum implementation:

- (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];
}
1 Answers
Related