ProgressView Whlle Get Api








Is there any way to get progress from dataURL in swift while the data is downloading, following this step
1. Create progress view, label and button in XIB file or storyboad.


2. Setting UIProgressView component like this below has the following propertiesProgressView 

  • progressTintColor – Used to change the UIColor of the progress part i.e. the filled part in the ProgressView.
  • trackTintColor – Used to change the UIColor of the track i.e. the unfilled part of the ProgressView.
  • ProgressBarStyle – There are two styles: default and bar. The bar style has a transparent track.
  • trackImage – Here an image is used instead of color for the unfilled part.
  • progressImage – Image is used to show the progress.



3. This step one to test your progressview

import UIKit

class LoadingViewController: UIViewController {
    @IBOutlet weak var progressView: UIProgressView!
    @IBOutlet weak var label: UILabel!

    let maxTime: Float = 100.0
    var currentTime: Float = 0.0

    override func viewDidLoad() {
        super.viewDidLoad()
        progressView.transform = progressView.transform.scaledBy(x: 1, y: 4)
    }

    @IBAction func actionB(_ sender: UIButton) {
        progressView.setProgress(currentTime, animated: true)
        perform(#selector(updateProgress), with: self, afterDelay: 1.0)
    }

    @objc func updateProgress() {
        currentTime = currentTime + 1.0
        progressView.progress = currentTime / maxTime
        label.text = "\(currentTime)"
        
        if currentTime < maxTime {
            perform(#selector(updateProgress), with: self, afterDelay: 1.0)
        } else {
            print("stop")
            currentTime = 0.0
        }
    }
}

4. ProgressView 
Image Download progress from API

/  Copyright © 2019 luffyselah. All rights reserved.
//

import UIKit

class LoadingViewController: UIViewController {
    @IBOutlet weak var progressView: UIProgressView!
    @IBOutlet weak var label: UILabel!

    var buffer: NSMutableData = NSMutableData()
    var session: URLSession?
    var dataTask: URLSessionDataTask?
    let url = NSURL(string: "http://i.stack.imgur.com/b8zkg.png")
    var expectedContentLength = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // for custom height progressview
        progressView.transform = progressView.transform.scaledBy(x: 1, y: 4)
    }

    @IBAction func actionB(_ sender: UIButton) {
        perform(#selector(updateProgress), with: self, afterDelay: 1.0)
    }

    @objc func updateProgress() {
        progressView.setProgress(0.0, animated: true)
        let configuration = URLSessionConfiguration.default
        let manqueue = OperationQueue.main
        session = URLSession(configuration: configuration, delegate: self, delegateQueue: manqueue)
        dataTask = session?.dataTask(with: NSURLRequest(url: url! as URL) as URLRequest)
        dataTask?.resume()
    }
}

extension LoadingViewController: URLSessionDelegate, URLSessionDataDelegate {
    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
        expectedContentLength = Int(response.expectedContentLength)
        print(expectedContentLength)
        completionHandler(URLSession.ResponseDisposition.allow)
    }

    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
        buffer.append(data as Data)

        let percentageDownloaded = Float(buffer.length) / Float(expectedContentLength)
        progressView.setProgress(percentageDownloaded, animated: true)
    }

    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        // use buffer here.Download is done
        progressView.setProgress(1.0, animated: true) // download 100% complete
    }
}





No comments: