2017-02-09 60 views
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]; 
} 

回答

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]; 
}