温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ios如何开发加载webview显示进度条

发布时间:2021-06-30 15:11:52 来源:亿速云 阅读:271 作者:小新 栏目:移动开发

这篇文章给大家分享的是有关ios如何开发加载webview显示进度条的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

WKWebView加载网页进度跳显示主要效果如下:

ios如何开发加载webview显示进度条

这里主要是使用KVO监听WKWebView的“estimatedProgress”属性,通过监听该属性的变化才是进度条的长度。

1、定义便利构造函数、以及属性和控件

var url: String?
  var progresslayer = CALayer()
  var webView: WKWebView?
  var button: UIButton?
convenience init(title: String, url: String) {
    self.init()
    self.title = title
    self.url = url
  }

2、创建webview控件,并监听estimatedProgress,进度条初始化的时候会给一定的长度显示(原因下面解释)。

func setupUI() {
    webView = WKWebView(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: screenHeight-64.0))

    if url == "" {
      url = "http:www.baidu.com"
    }
    let request = URLRequest(url: URL(string: url ?? "http:www.baidu.com")!)
    webView?.load(request)
    webView?.uiDelegate = self
    webView?.navigationDelegate = self;
    view.addSubview(webView!)

    //添加属性监听
    webView?.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
    progresslayer.frame = CGRect.init(x: 0, y: 0, width: screenWidth * 0.1, height: 3)
    progresslayer.backgroundColor = UIColor.green.cgColor
    view.layer.addSublayer(progresslayer)
  }

3、监听estimatedProgress属性变化,并修改进度条长度,创建进度条的时候之所以给一定的默认长度主要是因为在没有网络的状态下会立即进度float == 1条件,这样给人的感觉就是没有加载网页一样。

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "estimatedProgress" {
      progresslayer.opacity = 1
      let float = (change?[NSKeyValueChangeKey.newKey] as! NSNumber).floatValue
      progresslayer.frame = CGRect.init(x: 0, y: 0, width: (screenWidth * CGFloat(float)) , height: 3)
      if float == 1 {
        weak var weakself = self
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: {
          weakself?.progresslayer.opacity = 0
        })
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.8, execute: {
          weakself?.progresslayer.frame = CGRect.init(x: 0, y: 0, width: 0, height: 3);
        })
      }
    }else{
      super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    }
  }

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")
  }

感谢各位的阅读!关于“ios如何开发加载webview显示进度条”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI