WKWebView之彈窗提示與撥打電話 - WKWebView踩坑記錄

WKWebView默認(rèn)禁止了一些跳轉(zhuǎn)

  • UIWebView
    打開ituns.apple.com跳轉(zhuǎn)到appStore, 撥打電話, 喚起郵箱等一系列操作UIWebView默認(rèn)支持的.
  • WKWebView
    默認(rèn)禁止了以上行為,除此之外,js端通過alert()`彈窗的動作也被禁掉了.
    如何支持呢?
  • 首先要設(shè)置WKWebView的WKUIDelegate,并實(shí)現(xiàn)以下方法
- (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;
        } 
    }
    // 打開appstore
   if ([url.absoluteString containsString:@"ituns.apple.com"]) {
       if ([app canOpenURL:url]) {
          [app openURL:url];
          decisionHandler(WKNavigationActionPolicyCancel);
        return;
   }
decisionHandler(WKNavigationActionPolicyAllow);
}

支持alert()


- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
    // js 里面的alert實(shí)現(xiàn),如果不實(shí)現(xiàn),網(wǎng)頁的alert函數(shù)無效
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"確定"
                                                        style:UIAlertActionStyleCancel
                                                      handler:^(UIAlertAction *action) {
                                                          completionHandler();
                                                      }]];
    
    [self presentViewController:alertController animated:YES completion:^{}];

}

- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {
    //  js 里面的alert實(shí)現(xiàn),如果不實(shí)現(xiàn),網(wǎng)頁的alert函數(shù)無效  ,
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"確定"
                                                        style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction *action) {
                                                          completionHandler(YES);
                                                      }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消"
                                                        style:UIAlertActionStyleCancel
                                                      handler:^(UIAlertAction *action){
                                                          completionHandler(NO);
                                                      }]];
    
    [self presentViewController:alertController animated:YES completion:^{}];
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容