-(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(),则抛出expect
**[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没有.以下是代码我得到相同的异常,即使在ok和cancel按钮中调用了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全部内容,希望文章能够帮你解决ios – 解雇前调用WKWebView completionHandler所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)