无法从 Firebase 存储下载文件 swift 4.2
Can't download file from 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
中的路径,它是正确的。
我在这里错过了什么?
我假设您正在尝试读取实际的 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!")
}
}
}
}
我更改了 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
中的路径,它是正确的。
我在这里错过了什么?
我假设您正在尝试读取实际的 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!")
}
}
}
}