swift - 您如何调整存储规则以适应您的应用程序?
问题描述
前提:这些是目前的规则
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
当我运行上述规则时,并非所有用户的图像都会显示。我不知道是不是因为所有用户都在一部手机上(我自己测试,注销,登录,冲洗并重复),但我对此表示怀疑。这些规则中有一些东西(特别是 auth!not nil 部分)使得并非所有图像都返回。我说这是因为使用以下规则,每个用户都会获得一张图片(尽管并非总是他上传的图片,因为规则允许任何内容,但它表明代码可以从 firebase 获取图片给所有用户)。
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
这是从 firebase 获取图像的代码。
class homepage: UITableViewController, CLLocationManagerDelegate{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let databaseRef = Database.database().reference()
let uid = Auth.auth().currentUser!.uid
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
latestLocation = ["latitude" : locValue.latitude, "longitude" : locValue.longitude]
let lat = locValue.latitude
let lon = locValue.longitude
dict = CLLocation(latitude: lat, longitude: lon)
print("dict", dict)
if let locationDictionary = latestLocation { databaseRef.child("people").child(uid).child("Coordinates").setValue(locationDictionary)
}
}
......
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
let immy = cell.viewWithTag(1) as! UIImageView
let person: Userx = people[indexPath.row]
let like = cell.viewWithTag(3) as! UIButton
cell.lblName.text = person.Education
cell.postID = self.people[indexPath.row].postID
if let PhotoPosts = person.PhotoPosts {
let url = URL(string: PhotoPosts)
immy.sd_setImage(with: url)
}
return cell
}
……
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
table.delegate = self
........
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
if people.key != thisUsersUid {
print("peoplekey",people.key)
let peoplePhotoPosts = peopleObject?["PhotoPosts"] as? String
let peopleimageDownloadURL = peopleObject?["imageDownloadURL"] as? String
let peopl = Userx(............PhotoPosts: peoplePhotoPosts, imageDownloadURL: peopleimageDownloadURL, postID: peoplepostID, .........)
self.people.append(peopl)
} else {
print ("145454")
}
} else {
print ("alphaaa")
}
}
self.people.sort { ($0.distance ?? 0) < ($1.distance ?? 0)
}
print("aaaaaaaa", self.people.map {$0.distance})
self.table.reloadData()
}
}
})
……
extension UIImageView {
public func imageFromServerURL(urlString: String) {
self.image = nil
URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil {
print("error")
return
}
DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
self.image = image
})
}).resume()
}}
}
解决方案
推荐阅读
- python - 为什么这个python Lark语法这么慢?
- ios - 在 SwiftUI 中从 Firebase 读取实时数据库中的数据
- forms - 用于从 camsonline 获取综合账户报表的 API 调用
- sql-server - 用于过滤跨列/不同行的值列表的高效查询
- javascript - 如何使用 ES6 解构赋值将一个对象分配给另一个对象
- google-chrome-extension - 如何检查浏览器扩展内容脚本是否已加载到后台页面中包含的外部 iframe 上?
- spring-boot - 在浏览器中,我无法获得引导表,而是出现白标错误
- typescript - 为什么在使用 `if` 缩小 TypeScript 接口时需要关键字 `as`?
- python - 替换从某一行开始的列
- python - 在 Python 中将 Int 转换为字符串