Showing the file download progress with NSURLSessionDataTask

前端 未结 4 631
感情败类
感情败类 2020-12-01 06:49

I want to display file download progress (how many bytes are received) of particular file. It works fine with the NSURLSessionDownloadTask .My question is I want to achieve

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 07:13

    import Foundation
    import PlaygroundSupport
    
    let page = PlaygroundPage.current
    page.needsIndefiniteExecution = true
    
    let url = URL(string: "https://source.unsplash.com/random/4000x4000")!
    let task = URLSession.shared.dataTask(with: url) { _, _, _ in
      page.finishExecution()
    }
    
    // Don't forget to invalidate the observation when you don't need it anymore.
    let observation = task.progress.observe(\.fractionCompleted) { progress, _ in
      print(progress.fractionCompleted)
    }
    
    task.resume()
    

提交回复
热议问题