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 属性,你混淆了枚举和错误对象的属性。另外当前代码还有两处问题:
AuthErrorCode的初始化方式错误,正确做法是直接将NSError的code转换为AuthErrorCode.Code枚举值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
相关产品推荐
相关产品推荐

