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

时间:2022-05-07 14:40:45

很多app加载webview页面的时候都有进度条显示,今天我们这里主要使用相对轻量级的wkwebview加载网页,至于wkwebview 和uiwebview的区别与联系这里就不多讲了,自己百度哈哈。。。

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

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

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

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

?
1
2
3
4
5
6
7
8
9
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,进度条初始化的时候会给一定的长度显示(原因下面解释)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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条件,这样给人的感觉就是没有加载网页一样。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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加载失败后提示

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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、记载失败后点击提示重新加载

?
1
2
3
4
5
6
7
8
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监听,否则会出现内存泄露

?
1
2
3
deinit {
    webview!.removeobserver(self, forkeypath: "estimatedprogress")
  }

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

原文链接:http://www.jianshu.com/p/b9e18ddfc966?utm_source=tuicool&utm_medium=referral