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

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处理)