无法从Firebase Storage Swift 4.2下载文件

我更改了Firebase控制台上的权限,并设置为允许所有用户访问而无需身份验证。

我有以下代码:

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

我正在调用该函数,但是什么也没发生-我没有收到错误,但没有得到url(也尝试过getData却没有得到任何结果)。我三联检查了filePath中的路径,这是正确的。

我在这里想念什么?

haishizhende 回答:无法从Firebase Storage Swift 4.2下载文件

我假设您正在尝试读取实际的json文件,而不是清单路径中的所有文件

这是您的代码,其中包含有关如何修复的注释:

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!")
           }
        }
    }
}
本文链接:https://www.f2er.com/3101635.html

大家都在问