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()
}
}
Several thoughts:
- 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
}
来源:https://stackoverflow.com/questions/41495883/how-to-link-firebase-download-file-with-a-progress-view