CNWebViewController.swift 3.79 KB
//
//  CNWebViewController.swift
//  metaCode
//
//  Created by zx on 2023/9/21.
//

import Foundation
import WebKit

class CNWebViewController:CNLiveBaseViewController ,WKNavigationDelegate{
    
    lazy var webView: WKWebView = {
        let webView = WKWebView.init(frame: CGRectMake(0, 0, Width, Height))
        webView.scrollView.showsVerticalScrollIndicator = false
        webView.scrollView.showsHorizontalScrollIndicator = false
        webView.navigationDelegate = self
        return webView
    }()
    
    var _url:URL?
    var url:URL? {
        get{
            return _url
        }
        set{
            _url = newValue!
            if (_url != nil){
                let webUrl: URL! = _url!
                webView.evaluateJavaScript("navigator.userAgent") { [self] result, error in
                    let userAgent = result as! String
                    if error == nil && userAgent.count > 0 {
                        if !userAgent.contains(CNLiveEnvironmentManager.shared.base_web_UA) {
                            let newUserAgent = userAgent + CNLiveEnvironmentManager.shared.base_web_UA
                            webView.customUserAgent = newUserAgent
                        }
                    }
                    webView.load(URLRequest.init(url:webUrl))
                }
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarHidden = false
        self.view.addSubview(self.webView)
        webView.snp.makeConstraints { make in
            make.top.equalTo(get_system_nav_height())
            make.left.right.bottom.equalToSuperview()
        }
        
    }
    
    // MARK: - WKWebViewDelegate
    // 页面开始加载时调用
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("didStartProvisionalNavigation")
    }
    // 当内容开始返回时调用
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        print("didCommit")
    }
    // 页面加载完成之后调用
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("didFinish")
        titleName = webView.title!
    }
    // 页面加载失败时调用
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print("didFailProvisionalNavigation")
    }
    // 接收到服务器跳转请求之后调用
    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
        print("didReceiveServerRedirectForProvisionalNavigation")
    }
    // 在收到响应后,决定是否跳转 -> 默认允许
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        print("decidePolicyFor")
        //允许跳转
        decisionHandler(.allow)
        //不允许跳转
        //        decisionHandler(.cancel)
    }
    // 在发送请求之前,决定是否跳转 -> 默认允许
    @available(iOS 13.0, *)
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
        
        if navigationAction.navigationType == .other{
            //跳转Appstore
            let url = URL(string: "https://apps.apple.com/cn/app/%E7%BD%91%E5%AE%B6%E5%AE%B6%E5%95%86%E5%AE%B6%E7%89%88/id1337575497")
            if url == navigationAction.request.url {
                if UIApplication.shared.canOpenURL(url!) { UIApplication.shared.open(url!, options: [:], completionHandler: nil)
                }
            }
        }
        decisionHandler(.allow, preferences)
    }
}