ios开发加载webview显示进度条实例

2020-01-20 22:59:27刘景俊

4、web view加载失败后提示


extension KKWebView : WKUIDelegate, WKNavigationDelegate {
  func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
    guard let btn = button else {
      button = UIButton(type: .system)
      button?.frame = CGRect.init(x: 0, y: 3, width: screenWidth, height: screenHeight-64-3)
      button?.backgroundColor = UIColor.white
      button?.setTitleColor(UIColor.darkText, for: .normal)
      button?.setTitle("点击重新加载", for: .normal)
      button?.addTarget(self, action: #selector(loadURL), for: .touchUpInside)
      view.addSubview(button!)
      return
    }
    btn.isHidden = false
  }
}

5、记载失败后点击提示重新加载


 func loadURL() {
    button?.isHidden = true
    if url == "" {
      url = "http:www.baidu.com"
    }
    let request = URLRequest(url: URL(string: url ?? "http:www.baidu.com")!)
    webView?.load(request)
  }

5、移除监听,离开页面的时候需要移除KVO监听,否则会出现内存泄露


deinit {
    webView!.removeObserver(self, forKeyPath: "estimatedProgress")
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到IOS开发频道。