ios - 如何将 Firebase 下载文件与进度 View 链接?

标签 ios swift firebase firebase-storage uiprogressview

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

我想要做的是将其与带有百分比而不是事件指示器的下载进度 View 链接。

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
}

关于ios - 如何将 Firebase 下载文件与进度 View 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41495883/

相关文章:

swift - 为集合中的每个项目添加一个按钮

ios - 没有渲染/虚拟内容的 ARKit/SceneKit 截图

ios - Google placepicker 未在主线程警告上运行

ios - 改进 Cocos2D Spawn 系统?

ios - Swift:如何仅在代码执行后调用此完成 block ?

JavaScript JQuery 为什么按钮的 ID 没有被正确引用

ios - 根据位置对 UIView subview z 顺序进行排序

swift - 无法从 Firebase 获取值(value)

javascript - auth.onAuthStateChanged 在 vue3 和 firebase 9.0.0 beta.3 上不起作用

ios - 如何根据 Firestore 子集合中的值过滤根集合?