刚刚更新了我的可可豆,从而更新了firebase。这行代码是旧的方式,现在是错误的:
let downloadURL = metaData!.downloadURL()!.absoluteString
let values: Dictionary<String, Any> = ["download_url": downloadURL]
下面的代码是提取URL字符串的正确方法。然而,我需要帮助我如何将该字符串放入我的数组以保存到firebase。
storageRef.downloadURL(completion: {(url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
let downloadURL = url?.absoluteString
})
let values: Dictionary<String, Any> = ["download_url": downloadURL]
我小时候如何保存“价值观”
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((self.loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
if error != nil {
print("error saving post in db")
} else {
let storageRef = Storage.storage().reference().child("posts_requests").child((self.loggedInUser?.uid)!).child(snapshot.childSnapshot(forPath: "uid").value as! String).child(snapshot.childSnapshot(forPath: "uid").value as! String).child(snapshot.childSnapshot(forPath: "imageID").value as! String)
storageRef.delete(completion: { error in
if let error = error {
print(error)
} else {
print("Successful Delete")
}
})
}
}
使用下面的答案。。。
当我使用下面提交的答案时,我会得到一个打印输出,上面写着“用户没有访问gs://shoppeer-e7270.appspot.com/(null)的权限”。我所要做的就是获取这个URL字符串,并将它添加到我的“值”中,这是一个字典。
我的完整代码图片上传以及保存为一个孩子
let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
let usersRef = Database.database().reference().child("Businesses")
let databaseRef = Database.database().reference()
let imageName = NSUUID().uuidString
let photoRef = photosRef.child("\(uid)")
let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
var downloadURLSting = String()
photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
if let error = error {
print("there was an error")
print(error.localizedDescription)
return
} else {
// store downloadURL
storage.reference().downloadURL(completion: {(url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
let downloadURL = url?.absoluteString
let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
// store downloadURL at database
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
if error != nil {
print("error saving post in db")
} else {
// reset caption field
self.descriptionTextView.text = ""
// reset placeholder image
self.imageView.image = UIImage(named: "filterPlaceholder")
MBProgressHUD.hide(for: self.view, animated: true)
let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
self.present(viewConrolller, animated: true, completion: nil)
}
}
})
}
}
这个工作只是下载URL字符串为零
let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
let usersRef = Database.database().reference().child("Businesses")
let databaseRef = Database.database().reference()
let imageName = NSUUID().uuidString
let photoRef = photosRef.child("\(uid)")
let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
if let error = error {
print("there was an error")
print(error.localizedDescription)
return
} else {
// store downloadURL
photoRef.downloadURL(completion: {(url, error) in
if error != nil {
guard let downloadURL = url?.absoluteString else { return }
let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]
// store downloadURL at database
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
if error != nil {
print("error saving post in db")
} else {
// reset caption field
self.descriptionTextView.text = ""
// reset placeholder image
self.imageView.image = UIImage(named: "filterPlaceholder")
MBProgressHUD.hide(for: self.view, animated: true)
let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
self.present(viewConrolller, animated: true, completion: nil)
}
}
} else {
print(error!.localizedDescription)
print("error")
return
}
})
}
}