如何 link Firebase 下载带有进度视图的文件?

How to link Firebase download file with a progress view?

下面是我用来从 Firebase 存储下载 PDF 文件的代码。

我想要做的是 link 它带有百分比而不是 activity 指示器的下载进度视图。

import Foundation
import UIKit
import Firebase
import SwiftLoader


class showPdfVC: UIViewController , UIWebViewDelegate,ReaderViewControllerDelegate{


var pdfbooks = UIWebView()

var nIndex:NSInteger!
var post: Post!
var db : DBHelper = DBHelper()
var book : BookModel?


@IBAction func backbtn(_ sender: Any) {

    if let navController = self.navigationController {
        navController.popViewController(animated: true)
    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    var config : SwiftLoader.Config = SwiftLoader.Config()
    config.size = 150
    config.spinnerColor = .brown
    config.foregroundColor = .black
    config.foregroundAlpha = 0.5
    config.titleTextColor = .brown


    SwiftLoader.setConfig(config)


    if "" !=  book?.bookPath {
     //   self.activityIND.isHidden = true
      //  self.activityIND.stopAnimating()
        UIApplication.shared.isNetworkActivityIndicatorVisible = false

        SwiftLoader.hide()

        loadReader(filePaht: (book?.bookPath)!)
    } else {

        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let strName = book?.id
        let filePath = "\(documentsPath)/"+strName!+".pdf"
        let fileManager = FileManager.default
      //  self.activityIND.startAnimating()
         SwiftLoader.show(title: "Loading...", animated: true)
        UIApplication.shared.isNetworkActivityIndicatorVisible = true

        if fileManager.fileExists(atPath: filePath) {
        //    self.loadFromUrl(path: filePath)
            loadReader(filePaht: (book?.bookPath)!)
            return;
        }


        let reference = FIRStorage.storage().reference(forURL: (self.book?.bookURL)!)
        reference.data(withMaxSize: 50 * 1024 * 1024) { (data, error) -> Void in
            if (error != nil) {

                print ("unable to download pdf file from Firebase Storage")

            //    self.activityIND.isHidden = false
           //     self.activityIND.startAnimating()
                 SwiftLoader.show(title: "Loading...", animated: true)
                UIApplication.shared.isNetworkActivityIndicatorVisible = true

            } else {

                if ((try! data?.write(to: URL.init(fileURLWithPath: filePath, isDirectory: false))) != nil) {
             //       self.loadFromUrl(path: filePath)
                    print ("pdf file is downloaded from Firebase Storage")
                    self.db.upDate(id: (self.book?.id)!, bookPath: filePath)
                 //   self.activityIND.isHidden = true

                    SwiftLoader.hide()
                    self.loadReader(filePaht: filePath)

                    UIApplication.shared.isNetworkActivityIndicatorVisible = false



              }
            }
        }

    }
}


func loadReader(filePaht : String)  {

    let document = ReaderDocument(filePath: filePaht, password: nil)
    if document != nil {
        let readerVC = ReaderViewController(readerDocument: document)
        readerVC?.delegate = self
        readerVC?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
        readerVC?.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        self.navigationController?.pushViewController(readerVC!, animated: true)
    }

}

func dismiss(_ viewController: ReaderViewController!) {

   _ = self.navigationController?.popToRootViewController(animated: true)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

几个想法:

把它们放在一起,你会得到这样的东西:

// Create a reference to the file we want to download
let starsRef = storageRef.child("images/stars.jpg")

// Start the download (in this case writing to a file)
let downloadTask = storageRef.write(toFile: localURL)

// Start progress indicator

// Observe changes in status
downloadTask.observe(.progress) { snapshot in
  // Download reported progress
  let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
    / Double(snapshot.progress!.totalUnitCount)
  // Update the progress indicator
}

downloadTask.observe(.success) { snapshot in
  // Download completed successfully
  // Stop progress indicator
}

// Errors only occur in the "Failure" case
downloadTask.observe(.failure) { snapshot in
  // An error occurred!
  // Stop progress indicator
}