Idanis
Idanis

Reputation: 1988

Can't download file from Firebase Storage swift 4.2

I changed the permissions on the Firebase console and set to allow all users access without the need for an authentication.

I have the following code:

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
    FirebaseApp.configure()
    Utils.initApp()

    return true
}

Utils.swift

import Foundation
import Firebase

class Utils
{
    static var inventoryJsonString = "Inventory/Inventory.json"
    static var oneMB : Int64 = 1024 * 1024

    static func initApp()
    {
        getJsonDate(inventoryJsonString)
    }

    static func getJsonData(filePath: String)
    {        
        let storageRef = Storage.storage().reference()
        let jsonRef = storageRef.child("Inventory")

        jsonRef.getData(maxSize: self.oneMB)
        {
            extractedData, error in
            print("a")
            if let error = error{
                print("b")
        }
        else
        {
            print("c")
        }
    }
}

I'm calling that function but nothing happnes - I don't get an error, yet I'm not getting the url (also tried with getData and got nothing). I tripple checked the path in filePath and it's correct.

What am I missing here?

Upvotes: 1

Views: 387

Answers (1)

Jay
Jay

Reputation: 35657

I assume you're trying to read the actual json file, not all the files within the Inventory path

Here's your code with notes on how to fix:

class Utils
{
    static var inventoryJsonString = "Inventory/Inventory.json" //NOT USED!

    static var oneMB : Int64 = 1024 * 1024

    static func initApp() {
        getJsonDate(inventoryJsonString)
    }

    static func getJsonData(filePath: String) {  //filePath is NOT USED!    
        let storageRef = Storage.storage().reference()

        **this is a PATH to the enclosing directory only and not the JSON file**
        let enclosingPathRef = storageRef.child("Inventory")

        **add this to make it work**
        let actualFileRef = enclosingPathRef.child("Inventory.json")

        actualFileRef.getData(maxSize: self.oneMB) { extractedData, error in
           if let error = error{
              print("an error occurred: \(error.localizedDescription")
           } else {
              print("success!")
           }
        }
    }
}

Upvotes: 2

Related Questions