You need to enable JavaScript to run this app. iOS应用Facebook登录编译报错:AuthErrorCode无debugDescription成员
优惠活动
大模型
产品
解决方案
定价
更多

iOS应用Facebook登录编译报错:AuthErrorCode无debugDescription成员

问题:Facebook登录编译报错:AuthErrorCode 无 debugDescription 成员

我在实现应用的Facebook登录功能时,代码无法编译启动,抛出错误:Value of type 'AuthErrorCode' has no member 'debugDescription'。

相关代码如下:

extension SignInViewController {
    func facebookLogin() {
        let loginManager = LoginManager()
        loginManager.logIn(permissions: [.publicProfile, .email], viewController: self) { (loginResult) in
            switch loginResult
            {
            case .cancelled:
                self.present(UIAlertController.errorAlert(with: "Cancelled"), animated: true, completion: nil)
                break
            case .failed(let error):
                print(error.localizedDescription)
                self.present(UIAlertController.errorAlert(with: error.localizedDescription), animated: true, completion: nil)
                break
            case .success(let grantedPermission, let declinedPermission, let token):
                print("the grandted permissions \(grantedPermission)")
                print("the declined  permissions \(declinedPermission)")
                print("the token is \(token)")


                let credential = FacebookAuthProvider.credential(withAccessToken: token.tokenString)


                MBProgressHUD.showAdded(to: self.view, animated: true)
                // get Facebook user information
                self.getFacebookInfo({ (appUser) in
                    if let appUser = appUser {
                        // login Firebase with Facebook
                        OAuthProvider.credential(withProviderID: "facebook.com", idToken: token.tokenString, rawNonce:self.randomNonceString())
                        Auth.auth().signIn(with: credential) { (result, error) in
                            if let error = error {
                                    MBProgressHUD.hide(for: self.view, animated: true)
                                    let castedError = error as NSError
                                let firebaseError = AuthErrorCode(AuthErrorCode.Code(rawValue: castedError.code) ?? <#default value#>)
                                if firebaseError != nil {
                                    print(firebaseError.debugDescription)
                                }
                                self.present(UIAlertController.errorAlert(with: "Signin was failed. \n\(error.localizedDescription)"), animated: true, completion: nil)
                                return
                            }
                            appUser.userId = result?.user.uid
                            self.signedInWithFacebook((result?.user)!, appUser: appUser)
                            MBProgressHUD.hide(for: self.view, animated: true)
                        }
                    } else {
                        MBProgressHUD.hide(for: self.view, animated: true)
                        self.present(UIAlertController.errorAlert(with: "Cancelled"), animated: true, completion: nil)
                    }
                })

            }
        }
    }

报错的代码段:

if firebaseError != nil {
print(firebaseError.debugDescription)
}

报错信息:Value of type 'AuthErrorCode' has no member 'debugDescription',求解决办法。


解决方法

问题根源

AuthErrorCode 是Firebase Auth的枚举类型,本身并没有 debugDescription 属性,你混淆了枚举和错误对象的属性。另外当前代码还有两处问题:

  1. AuthErrorCode 的初始化方式错误,正确做法是直接将NSError的code转换为AuthErrorCode.Code枚举值
  2. firebaseError 是非可选类型,if firebaseError != nil 的判断完全多余

修改后的代码

将报错的代码段替换为以下内容:

MBProgressHUD.hide(for: self.view, animated: true)
let castedError = error as NSError
// 转换为AuthErrorCode.Code枚举并打印
if let firebaseErrorCode = AuthErrorCode.Code(rawValue: castedError.code) {
    print("Firebase错误码:\(firebaseErrorCode.rawValue)")
    print("错误类型:\(firebaseErrorCode)")
}
// 直接使用原错误对象的debugDescription
print(error.debugDescription)
self.present(UIAlertController.errorAlert(with: "Signin was failed. \n\(error.localizedDescription)"), animated: true, completion: nil)
return

额外优化提示

你代码中这一行是无效代码,创建了OAuth凭证但未使用:

OAuthProvider.credential(withProviderID: "facebook.com", idToken: token.tokenString, rawNonce:self.randomNonceString())

如果需要用这个凭证登录Firebase,应该替换掉之前的credential变量,或者直接将其传入signIn(with:)方法中。

内容的提问来源于stack exchange,提问作者Sid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 21:10:29