2017-01-05 3 views
0

Unten ist der Code, den ich verwende, um PDF-Dateien vom Firebase-Speicher herunterzuladen.Wie verlinke ich die Firebase-Download-Datei mit einer Fortschrittsanzeige?

Ich möchte es mit einer Download-Fortschrittsanzeige mit Prozentsatz statt Aktivitätsindikator verknüpfen.

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() 
} 
} 

Antwort

2

Mehr Gedanken:

  • downloaden Sie nicht im Speicher und dann auf der Festplatte schreiben - wir haben eine Methode, die downloads straight to the file system
  • Zuhörer Bringen statt einem einzigen Abschluss Handler - auf diese Weise Sie können listen to progress updates
  • Verwendung so etwas wie MBProgressHUD einen inkrementellen Fortschrittsbalken
  • Keine Notwendigkeit zu zeigen, dieeinstellenwie wir das für Sie zusammen

dass Putting tun werden, erhalten Sie so etwas wie:

// 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 
} 
Verwandte Themen