ios - Firebase 访问被拒绝?

标签 ios swift firebase

所以我有这个小代码片段负责将图片上传到 firebase 存储。出于某种奇怪的原因,我的访问一直被拒绝。 storageRef 似乎有问题,因为它进入了打印出错误的 if 语句。

  // will handle the  sign up of a user
    @objc func handleSignUp(){
        // first we cant to take sure that all of the fields are filled
        var profilePic: String = ""

        // will take the user selected image and load it to firebase
        let imageName = NSUUID().uuidString
 let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).PNG")
        if let userImage = selectedImageFromPicker,let uploadData = UIImageJPEGRepresentation(userImage, 0.1){

            storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
                if error != nil{
                    print(error ?? "")
                    return
                }
                 profilePic = (metadata?.downloadURL()!.absoluteString)!
                guard let username = self.nameTextField.text,
                    let confirmPassword = self.confirmPasswordTextField.text,
                    let email = self.emailTextField.text,
                    let password = self.passwordTextField.text,
                    !username.isEmpty,
                    !email.isEmpty,
                    !password.isEmpty,
                    !confirmPassword.isEmpty
                    else {

                        print("Required fields are not all filled!")
                        return
                }
                if self.validateEmail(enteredEmail:email) != true{
                    let alertController = UIAlertController(title: "Error", message: "Please Enter A Valid Email", preferredStyle: .alert)
                    let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)
                    self.present(alertController, animated: true, completion: nil)
                }

                // will make sure user is validated before it even tries to create user

                // will make sure the password and confirm password textfields have the same value if so it will print an error
                if self.passwordTextField.text != self.confirmPasswordTextField.text {
                    let alertController = UIAlertController(title: "Error", message: "Passwords Don't Match", preferredStyle: .alert)
                    let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)
                    self.present(alertController, animated: true, completion: nil)
                }

                // will authenticate a user into the authentication services with an email and passowrd
                AuthService.createUser(controller: self, email: email, password: password) { (authUser) in
                    guard let firUser = authUser else {
                        return
                    }
                    //will add user to the database
                    print(profilePic)
                    print(username)
                    UserService.create(firUser, username: username , profilePic: profilePic,location: self.userLocation!) { (user) in
                        guard let user = user else {
                            print("User successfully loaded into firebase db")
                            return
                        }
                        // will set the current user for userdefaults to work
                        print(user.profilePic)
                        print(user.username)

                        User.setCurrent(user, writeToUserDefaults: true)
                        // self.delegate?.finishSigningUp()

                        self.finishSigningUp()


                    }
                }

        })
        }
    }

错误是

Error Domain=FIRStorageErrorDomain Code=-13021 "User does not have permission to access gs://eventful-3d558.appspot.com/profile_images/C0BC898A-6A6F-490F-954B-51D705CD2B23.PNG." UserInfo={object=profile_images/C0BC898A-6A6F-490F-954B-51D705CD2B23.PNG, bucket=eventful-3d558.appspot.com, ResponseBody={
  "error": {
    "code": 403,
    "message": "Permission denied. Could not perform this operation"
  }

如果有人知道它是什么,我将不胜感激。它阻止我注册用户。

最佳答案

你必须在Database->Rules中设置Database的规则接受所有读写

{
  "rules": {
  ".read": true,
  ".write": true
  }
}

通过将数据库读写规则设置为true,您将允许所有的读写请求。

如果您在 Firebase 存储中发现一些权限错误,请更改 Firebase 存储-> 规则

service firebase.storage {
  match /b/{bucket}/o {
  match /{allPaths=**} {
    allow read, write: if true;
    }
  }
}

关于ios - Firebase 访问被拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46981241/

相关文章:

ios - 在 Objective C 中用字符串替换正则表达式?

objective-c - 如何将视频(图像)输入与外部源同步?

ios - 是否可以在 UINavigationContoller 堆栈上推送 2 个单独的 UIViewController 实例

ios - UIImageView 无法以编程方式更改位置

ios - 向左滑动 iPhone 主屏幕,XCode UI 测试

javascript - Firebase 数组未在 AngularJS View 中渲染

ios - 从 Nib 加载多个自定义单元格

ios - 如果我将sdk文件夹放在react-native项目中,Facebook sdk无法正常工作吗?

javascript - 未捕获的语法错误 : Identifier 'baseUrl' has already been declared

ios - Firebase 分析 iOS