WKWebView不显示JS的Alert,Confirm,Tex

作者: 傅hc | 来源:发表于2018-02-01 15:27 被阅读8次

    最新项目使用网页加载页面的基本上都由UIWebView转到WKWebView了,然后刚刚才发现用了WKWebView之后不会弹窗提示了,查了一下知道原来WKWebView默认禁止了下面的跳转:

    • 打开itunes.apple.com跳转到App Store, 拨打电话, 唤起邮箱等一系列操作
    • JSAlert,Confirm,TextInput弹框

    解决方法:
    首先实现WKUIDelegate代理方法

    解决唤不起打电话和跳转系统应用的方法

    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
        NSURL *URL = navigationAction.request.URL;
        NSString *scheme = [URL scheme];
        UIApplication *app = [UIApplication sharedApplication];
        // 打电话
        if ([scheme isEqualToString:@"tel"]) {
            if ([app canOpenURL:URL]) {
                [app openURL:URL];
                // 一定要加上这句,否则会打开新的页面
                decisionHandler(WKNavigationActionPolicyCancel);
                return;
            }
        }
        // 打开App Store
        if ([url.absoluteString containsString:@"ituns.apple.com"]) {
            if ([app canOpenURL:url]) {
                [app openURL:url];
                decisionHandler(WKNavigationActionPolicyCancel);
                return;
            }
            decisionHandler(WKNavigationActionPolicyAllow);
        }
    }
    

    解决JSAlert,Confirm,TextInput不弹框的方法

    #pragma mark -  Alert弹窗
    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message ? : @"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            completionHandler();
        }];
        [alertController addAction:action];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    #pragma mark -  Confirm弹框
    - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message ? : @"" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            completionHandler(NO);
        }];
        
        UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            completionHandler(YES);
        }];
        
        [alertController addAction:cancelAction];
        [alertController addAction:confirmAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    #pragma mark -  TextInput弹框
    - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler {
        
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:prompt message:@"" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.text = defaultText;
        }];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            completionHandler(alertController.textFields[0].text ? : @"");
        }];
        [alertController addAction:action];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    

    相关文章

      网友评论

        本文标题:WKWebView不显示JS的Alert,Confirm,Tex

        本文链接:https://www.haomeiwen.com/subject/cqmdzxtx.html