美文网首页
swift App中的webview加载url的code=403

swift App中的webview加载url的code=403

作者: 陈江红 | 来源:发表于2018-08-10 17:26 被阅读81次

App中加载url出现403并不会在fail代理函数中调用,会走finish的函数回调,所以不能判断出加载的地址是否出现403错误

在代理函数中调用

funcwebView(_webView:WKWebView, decidePolicyFor navigationAction:WKNavigationAction, decisionHandler:@escaping(WKNavigationActionPolicy) ->Void) {

        // 判断是否是网页跳转

        if(navigationAction.request.url?.scheme?.hasPrefix("http"))!{

            var response:URLResponse? =nil

            // 请求该链接,通过返回的statusCode判断是否异常

            let data =try?NSURLConnection.sendSynchronousRequest(navigationAction.request, returning: &response)

            if((response as?HTTPURLResponse)?.statusCode == 200){

                // 网页正常

                decisionHandler(.allow)

            }else{

                // 网页异常

                decisionHandler(.cancel)

            }

        }else{

            decisionHandler(WKNavigationActionPolicy.cancel)

                // 跳转到其他app

//            debugPrint("scheme = \(String(describing: navigationAction.request.url))");

            letscheme :URL= (navigationAction.request.url)!

            if (UIApplication.shared.canOpenURL(scheme)){

                UIApplication.shared.openURL(scheme)

            }

        }

    }

相关文章

网友评论

      本文标题:swift App中的webview加载url的code=403

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