0
錯誤:警報(確認(「?OK」))不能在WKWebview做工精細
Warning: Attempt to present on which is already presenting
似乎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];
}