[英]Firebase Database setValue: or removeValue failed: permission_denied iOS
[英]failed: permission_denied when setValue: or removeValue: in Firebase Database | Swift 4
我在 stackoverflow 中看到了很多答案,但沒有一個對我有用!
這是我的代碼:
import UIKit
import Firebase
class ViewController: UIViewController {
@IBOutlet var txt: UITextField!
var ref = DatabaseReference.init()
override func viewDidLoad() {
super.viewDidLoad()
loginAnony()
self.ref = Database.database().reference()
}
func loginAnony(){
Auth.auth().signInAnonymously(){
(user,error) in
if let error = error{
print(error.localizedDescription)
}else{
print("User ID\(user?.user.uid)")
}
}
}
@IBAction func btnClick(_ sender: Any) {
self.ref.child("chat").setValue("Yogesh")
}
}
我的數據庫規則:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
我已經刪除了:
{
"rules": {
".read": true,
".write": true
}
}
但它仍然無法正常工作。 誰能幫我這個?
Firebase 需要像這樣在您的 AppDelegate 中配置
func application(_ application: UIApplication, didFinishLaunchingWithOptions... {
FirebaseApp.configure()
然后在您的 viewController 中設置一個類 var:
class ViewController: UIViewController{
var ref: DatabaseReference!
所以,不要這樣做: var ref = DatabaseReference.init()
然后在 viewDidLoad 中,初始化類 var 然后進行身份驗證
override func viewDidLoad() {
super.viewDidLoad()
self.ref = Database.database().reference()
//proceed to authenticate
此外, Firestore規則不適用於 Firebase 實時數據庫,它有自己的一套規則。
service cloud.firestore //doesn't do anything to RTDB
因此,將您的 Firebase 數據庫規則保留為默認值
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.