Downloading file names from firebase storage

江枫思渺然 提交于 2020-01-24 21:20:29

问题


As i read in the documentation i can access single url in firebase storage like this:

`// Create a reference to the file you want to download 
let starsRef = storageRef.child("images/stars.jpg") 
// Fetch the download URL starsRef.downloadURL { url, error in 
   if let error = error { 
   // Handle any errors } 
   else { 
   // Get the download URL for 'images/stars.jpg' 
} }` 

However, i have many files there, so how can i skip giving direct path and instead iterate through all files in the given directory?

Thanks for tips.


回答1:


DownloadURL takes single string at a time. In case you want to show all the files inside a folder to a tableview like me, here is the full code:

   import UIKit import Firebase

My very First View Controller-

   class FolderList: UIViewController {
       var folderList: [StorageReference]?
        lazy var storage = Storage.storage()

       @IBOutlet weak var tableView : UITableView!

       override func viewDidLoad() {
           super.viewDidLoad()
   self.storage.reference().child("TestFolder").listAll(completion: {
   (result,error) in
               print("result is \(result)")
               self.folderList = result.items
               DispatchQueue.main.async {
                   self.tableView.reloadData()
               }
           })
       } }
   extension FolderList : UITableViewDataSource {
       func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return folderList?.count ?? 0
       }

       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           guard let cell = tableView.dequeueReusableCell(withIdentifier: "FolderListCell", for:
   indexPath) as? FolderListCell else {return UITableViewCell()}
           cell.itemName.text = folderList?[indexPath.row].name
           return cell
       }

       func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           return 64.0
       } }

   extension FolderList : UITableViewDelegate {
       func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
           let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
           guard let downloadVC = storyBoard.instantiateViewController(withIdentifier:
   "DownloadedItemView") as? DownloadedItemView else {
               return
           }
           downloadVC.storageRef = folderList?[indexPath.row]
           self.navigationController?.pushViewController(downloadVC, animated: true)
       } 
}

You each cell:

   class FolderListCell: UITableViewCell {

       @IBOutlet weak var itemName : UILabel!

   }


来源:https://stackoverflow.com/questions/46832504/downloading-file-names-from-firebase-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!