WKWebView基本使用

创建,我们可以在Viewcontroller中使用如下的懒加载的方式创建一个WKWebView 以及一个便于观察进度的进度条:

// wkWebView
    lazy var wkWebView = WKWebView()
    // 进度条
    lazy var progressView = UIProgressView()

随后我们可以在viewDidLoad实现方法,将这两个控件添加到视图中,在这里要注意iphone机型的的适配,代码如下:

override func viewDidLoad() {
        super.viewDidLoad()
     
        initInterface()
        
    }


    func initInterface() -> () {
        
        self.wkWebView.frame = CGRect(x: safeAreaInsets.left
            , y: safeAreaInsets.top + 44
            , width: ScreenWidth
            , height: ScreenHeight - safeAreaInsets.top - 44 - safeAreaInsets.bottom)
        let url = URL(string: "https://blog.csdn.net/kmonarch/article/details/82753235")
        let request = URLRequest(url: url!)
        wkWebView.navigationDelegate = self
        wkWebView.load(request)
        self.view.addSubview(self.wkWebView)
      

        self.progressView.frame = CGRect(x: safeAreaInsets.left
            , y: safeAreaInsets.top + 44
            , width: ScreenWidth
            , height: 2)
        progressView.progressTintColor = UIColor.red
        progressView.trackTintColor = UIColor.clear
        self.view.addSubview(self.progressView)
    }

代理实现方法

extension TestViewController: WKNavigationDelegate
{
    
    // 监听网页加载进度
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        self.progressView.progress = Float(self.wkWebView.estimatedProgress)
    }
    
    // 页面开始加载时调用
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        Log4jMessage(message: "开始加载...")
    }
    
    // 当内容开始返回时调用
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!){
        Log4jMessage(message: "当内容开始返回...")
    }
    
    // 页面加载完成之后调用
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){
        Log4jMessage(message: "页面加载完成...")
        /// 获取网页title
        self.title = self.wkWebView.title
        
        UIView.animate(withDuration: 0.5) {
            self.progressView.isHidden = true
        }
    }
    
    // 页面加载失败时调用
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error){
        Log4jMessage(message: "页面加载失败...")
        UIView.animate(withDuration: 0.5) {
            self.progressView.progress = 0.0
            self.progressView.isHidden = true
        }
        /// 弹出提示框点击确定返回
        let alertView = UIAlertController.init(title: "提示", message: "加载失败", preferredStyle: .alert)
        let okAction = UIAlertAction.init(title:"确定", style: .default) { okAction in
            _=self.navigationController?.popViewController(animated: true)
        }
        alertView.addAction(okAction)
        self.present(alertView, animated: true, completion: nil)
    }
 
}

基本方法和属性

(1)loadRequest()  加载请求
(2)goBack()  网页后退
(3)goForward()  网页前进
(4)reload()  网页重新加载
(5)stopLoading()  网页停止加载  
(6)title  网页标题
(7)canGoBack  网页是否能够后退
(8)canGoForward  网页是否能够前进
(9)estimatedProgress  网页加载中当前的进度

作者:酸奶Color
链接:https://www.jianshu.com/p/d29384454a9a
来源:
著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

WKUIDelegate

(1)WKWebView创建初始化加载的一些配置
func webView(webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, forNavigationAction navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?

(2)iOS9.0中新加入的,处理WKWebView关闭的时间
func webViewDidClose(webView: WKWebView)

(3)处理网页js中的提示框,若不使用该方法,则提示框无效
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void)

(4)处理网页js中的确认框,若不使用该方法,则确认框无效
func webView(webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: (Bool) -> Void)

(5)处理网页js中的文本输入
func webView(webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: (String?) -> Void)

WKNavigationDelegate

(1)决定网页能否被允许跳转
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void)

(2)处理网页开始加载
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)

(3)处理网页加载失败
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError)

(4)处理网页内容开始返回
func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!)

(5)处理网页加载完成
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!)

(6)处理网页返回内容时发生的失败
func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError)

(7)处理网页进程终止
func webViewWebContentProcessDidTerminate(webView: WKWebView)

你可能感兴趣的:(WKWebView基本使用)