ios - WKWebView completionHandler 在解雇前调用

标签 ios objective-c wkwebview wkwebviewconfiguration

我正在使用 WKUIDelegate 这个函数来处理 javascript 警报

-(void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{

    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Test Alert", nil)
                                                     message:message
                                                    delegate:self
                                           cancelButtonTitle:nil
                                           otherButtonTitles:@"OK", nil] autorelease];

    [alert show];
    completionHandler();
}

根据 Apple 文档,我们应该在按下 OK 按钮后调用警报的 compeletionHandler(),如前所述 here

按下 OK 按钮后如何调用 completionHandler()?如果我不调用 completionHandler() 则抛出预期

**[WKWebViewController webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:
    completionHandler:]:
***** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
   reason: 'Completion handler passed to -[WKWebViewController
   webView:runJavaScriptAlertPanelWithMessage:
   initiatedByFrame:completionHandler:] was not called'****

更新:

下面 Stefan 提到的解决方案在 JS Alert 上运行良好,但在 JS Confirm 上运行不佳。以下是我得到相同异常的代码,即使在确定和取消按钮中调用了 completionHandler()。

-(void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler
{
    MKCLOG_DEBUG(@"%@", frame.request.URL);
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:
                                NSLocalizedString(@"Test", nil) message: message
                                                            preferredStyle: UIAlertControllerStyleAlert];

    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:NSLocalizedString(@"Cancel", @"")
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action)
                                   {
                                       MKCLOG_DEBUG(@"Cancel action");
                                       completionHandler(NO);
                                   }];

    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action)
                               {
                                   MKCLOG_DEBUG(@"OK action");
                                   completionHandler(YES);
                               }];

    [alert addAction:cancelAction];
    [alert addAction:okAction];
}

最佳答案

您的代码现在设置的方式是,您显示 UIAlertView立即运行completionHandler()。两者同时发生。

你应该做的是这样的:

UIAlertController* alert = [UIAlertController alertControllerWithTitle:
    NSLocalizedString(@"Test Alert", nil) message: message
        preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle: @"OK"
    style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
      completionHandler();
}]; 
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

这将显示警报并在用户关闭时调用 completionHandler

请注意,我使用的是 UIAlertController,它仅适用于 iOS 8 及更高版本,但应该没问题,因为您依赖于 WKWebView

关于ios - WKWebView completionHandler 在解雇前调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32891812/

相关文章:

ios - AVPlayerViewController - UIButton 不触发视频

ios - NSFetchedResultsController 委托(delegate)奇怪的行为

javascript - 如何在WKWebView中调用带参数/不带参数的JavaScript函数?

ios - 如何使用一个wkwebview在网页之间进行标签栏导航?

ios - Swift 从 AnyClass 对象声明类型化数组

ios - JSON POST 在 iO 中不起作用

ios - 设置多个排序描述符

ios - 如何为具有随机位置的项目添加约束

ios - 我可以将适用于 iOS 的 GoogleMaps SDK 链接到除 -ObjC 之外的链接器标志吗

WKWebView 中的 iOS Objective-C LocalStore