错误:
警告:尝试呈现已经呈现的内容
似乎alert(confirm('ok?'));
样式代码触发器在WKWebview的委托中同时呈现视图控制器,有没有办法支持这个?
我的HTML:
<button onclick="alert(1);">alert</button><br>
<button onclick="alert(confirm('ok?'));">confirm</button><br>
<button onclick="alert(prompt('please input'));">text</button><br>
我的代码:
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
NSLog(@"runJavaScriptAlertPanelWithMessage:%@", message);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"TITLE" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"runJavaScriptAlertPanelWithMessage:%@, did clicked", message);
}];
[alert addAction:action];
completionHandler();
[self presentViewController:alert animated:YES completion:nil];
}
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{
NSLog(@"runJavaScriptAlertPanelWithMessage:%@", message);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"TITLE" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"runJavaScriptConfirmPanelWithMessage:%@, did clicked yes", message);
}];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"runJavaScriptConfirmPanelWithMessage:%@, did click cancel", message);
}];
[alert addAction:action];
[alert addAction:actionCancel];
completionHandler(YES);
[self presentViewController:alert animated:YES completion:nil];
}
答案 0 :(得分:0)
你应该在每个动作回调中使用completionHandler
,而不是仅仅把它放在委托主体中,所以它可能是这样的:
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{
NSLog(@"runJavaScriptAlertPanelWithMessage:%@", message);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"runJavaScriptConfirmPanelWithMessage:%@, did clicked", message);
completionHandler(YES);
}];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"runJavaScriptConfirmPanelWithMessage:%@, did click cancel", message);
completionHandler(NO);
}];
[alert addAction:action];
[alert addAction:actionCancel];
[self presentViewController:alert animated:YES completion:nil];
}