本文介绍了如何将Firebase下载文件与进度视图相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我用来从Firebase存储下载PDF文件的代码。
Below is the code I am using to download PDF files from Firebase storage.
我想要做的是将其与百分比的下载进度视图相关联活动指标。
What I want to do is to link it with a download progress view with percentage instead of activity indicator.
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()
}
}
推荐答案
几点想法:
- 不要在内存中下载然后写入磁盘 - 我们有一种方法可以
- 附加监听器而不是单个完成处理程序 - 这样你就可以
- 使用显示增量进度条
- 无需设置
isNetworkActivityIndicatorVisible
我们会为你做这件事
- Don't download in memory and then write to disk--we have a method that downloads straight to the file system
- Attach listeners rather than a single completion handler--that way you can listen to progress updates
- Use something like MBProgressHUD to show an incremental progress bar
- No need to set the
isNetworkActivityIndicatorVisible
as we'll do that for you
把这些放在一起,你会得到类似的东西:
Putting that together, you get something like:
// 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
}
这篇关于如何将Firebase下载文件与进度视图相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!