gpt4 book ai didi

ios - WKWebView completionHandler 在解雇前调用

转载 作者:可可西里 更新时间:2023-11-01 05:33:25 25 4
gpt4 key购买 nike

我正在使用 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/

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