firebase '权限被拒绝:缺少权限或权限不足,'

r7xajy2e  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(280)

我是一个编码新手。在SwiftUIKit中编写代码时,我很难将文档上传到Firestore数据库。程序给我的警告消息为

  • '权限被拒绝:缺少权限或权限不足。'/failed:权限缺失或不足*

我已经检查了消息,并尝试了所有的解决方案,但仍然无法修复它。我已经改变了规则在Firebase很多次,最后一个是这样的;

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null
    
    }
  }
}

这是我的代码,应用程序应该运行/上传数据到数据库,但它没有。我希望你能帮助我...谢谢你:)

import UIKit
import Firebase
import FirebaseStorage

class UploadViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var yorumTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        imageView.isUserInteractionEnabled = true
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(gorselSec))
        imageView.addGestureRecognizer(gestureRecognizer)
    }
    
    @objc func gorselSec() {
        
        let pickerController = UIImagePickerController()
        pickerController.delegate = self
        pickerController.sourceType = .photoLibrary
        present(pickerController, animated: true, completion: nil)
 
        
        }
        
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        imageView.image = info[.originalImage] as? UIImage
        self.dismiss(animated: true, completion: nil)
    }
   
    
    
    @IBAction func yukleTiklandi(_ sender: Any) {
        
        let storage = Storage.storage()
        let storageReference = storage.reference()
        let mediaFolder = storageReference.child("media")
        if let data = imageView.image?.jpegData(compressionQuality: 0.5){
            let uuid = UUID().uuidString
            let imageReference = mediaFolder.child("\(uuid).jpg")
            imageReference.putData(data) { storagemetadata, error in
                if error != nil {
                    self.hataMesajiGoster(title: "Hata", message: error?.localizedDescription ?? "Hata Aldınız, tekrar deneyin.")
                } else {
                    imageReference.downloadURL { [self] url, error in
                        if error == nil {
                            let imageUrl = url?.absoluteString
                            if let imageUrl = imageUrl {
                                let firestoreDatabase = Firestore.firestore()
                                let firestorePost = ["gorselurl": imageUrl, "yorum" : yorumTextField.text!, "email" : Auth.auth().currentUser?.email, "tarih" : FieldValue.serverTimestamp()]
                                
                                firestoreDatabase.collection("Post").addDocument(data: firestorePost) { (error) in
                                    
                                    if error != nil {
                                        self.hataMesajiGoster(title: "Hata", message: error?.localizedDescription ?? "Hata Aldınız, Tekrar Deneyiniz!")
                                    } else {
                                        
                                    }
                                }
                            }
                            
                        }
                    }
                }
            }
            
        }
        
        
    }
    
    func hataMesajiGoster(title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        let okayButton = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
        alert.addAction(okayButton)
        self.present(alert, animated: true, completion: nil)
    }
s5a0g9ez

s5a0g9ez1#

我不会为Swift编写代码,但我想我可以帮助您解决这个问题。
从你的代码看起来你是在处理firebase存储而不是firestore。我建议你使用下面的规则在本地测试:
第一个月
有了它,即使没有用户登录,您也可以进行测试。
如果你想继续使用你的规则,我建议你在代码级别检查用户是否登录了,因为我对swift了解不多,我会给予你一个文档链接,你可以在那里找到语法。
Get the currently signed-in user - Firebase

相关问题