WKWebView

WKWebView 使用需要支持到网络

//

//  GNWebViewViewController.swift

//  Notes

//

//  Created by boniu on 2021/7/13.

//

importUIKit

importWebKit

class GNWebViewViewController: UIViewController, WKNavigationDelegate {

    var titleStr = ""

    var locUrl = "";

    var httpUrl = "";

    var webView = WKWebView()

    override func viewDidLoad() {

        super.viewDidLoad()

        // 导航

        let statusBarManager = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.windowScene?.statusBarManager

        letstatusH = statusBarManager?.statusBarFrame.height??0

        lethomeTopView =GNHomeTopView.init(frame:CGRect.init(x:0, y:0, width:zScreenW, height: statusH+50))

        homeTopView.leftButton.addTarget(self, action: #selector(clickCloseBut), for: .touchUpInside)

        homeTopView.leftButton.isHidden=false;

        homeTopView.rightButton.isHidden=true;

        homeTopView.imgView.isHidden=true;

        homeTopView.leftButton.setTitle("确认", for: .normal)

        homeTopView.backgroundColor = UIColor(hex: "#F8F8F8")

        view.addSubview(homeTopView)

        // Do any additional setup after loading the view.


        self.webView.navigationDelegate = self;

        self.webView.frame=CGRect.init(x:0, y: homeTopView.height+homeTopView.y, width:zScreenW, height:zScreenH-homeTopView.height-homeTopView.y);


        self.loadRequest();

        view.addSubview(self.webView)

    }


    @objc func clickCloseBut() {

        self.dismiss(animated:true, completion:nil)

    }


    ///请求数据

    func loadRequest() {

        // 本地

        if locUrl.count > 0 {

            varpath:String?

            path =Bundle.main.path(forResource:locUrl, ofType:nil)

            leturl =URL(fileURLWithPath: path ??"")

            letrequest =URLRequest(url: url)

            webView.load(request)

            return

        }


//        let url = URL(string:"https://www.baidu.com")

        let url = URL(string:"https://static.rhinox.cn/html/privacy/privacy_xnote.html?appName=Xnotes6")

//        let url = URL(string:"https://www.up360.com")

//        webView.loadHTMLString("", baseURL: url!)

        letrequest =NSURLRequest.init(url: url!)

        webView.load(request as URLRequest)

//        webView.backgroundColor = UIColor.red

    }

    //页面开始加载时调用

    funcwebView(_webView:WKWebView, didStartProvisionalNavigation navigation:WKNavigation!) {


    }

    //当内容开始返回时调用

    funcwebView(_webView:WKWebView, didCommit navigation:WKNavigation!) {


    }

    // 页面加载完成之后调用

    funcwebView(_webView:WKWebView, didFinish navigation:WKNavigation!) {


    }

    //页面加载失败时调用

    funcwebView(_webView:WKWebView, didFail navigation:WKNavigation!, withError error:Error) {


    }

//    // 接收到服务器跳转请求之后调用

//    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {

//

//    }

//    // 在收到响应后,决定是否跳转

//    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void)  {

//

//    }

//    // 在发送请求之前,决定是否跳转

//    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

//

//    }


    //权限认证的方法 如果https 没有被认证的话走这里

    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {


        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {

            let card = URLCredential.init(trust: challenge.protectionSpace.serverTrust!);

            completionHandler(URLSession.AuthChallengeDisposition.useCredential,card);

        }

    }

}

你可能感兴趣的:(WKWebView)