Firebase google 登录身份验证 AppDelegate-使用未解析的标识符“isMFAEnabled”
Posted
技术标签:
【中文标题】Firebase google 登录身份验证 AppDelegate-使用未解析的标识符“isMFAEnabled”【英文标题】:Firebase google signin authentication AppDelegate- Use of unresolved identifier 'isMFAEnabled' 【发布时间】:2020-10-18 19:11:17 【问题描述】:我是 ios 开发新手。我正在尝试将谷歌登录添加到我的应用程序中,但我遇到了一些问题。代码显示一些“使用未解析的标识符'isMFAEnabled”和“'AppDelegate' 类型的值没有成员'showTextInputPrompt'”。请帮助我。我正在关注这个文档-https://firebase.google.com/docs/auth/ios/google-signin#swift_9enter image description here
import UIKit
import Firebase
import GoogleSignIn
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
FirebaseApp.configure()
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
return true
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
return GIDSignIn.sharedInstance().handle(url)
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!)
if let error = error
print(error.localizedDescription)
return
guard let authentication = user.authentication else return
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) (authResult, error) in
if let error = error
let authError = error as NSError
if (isMFAEnabled && authError.code == AuthErrorCode.secondFactorRequired.rawValue)
// The user is a multi-factor user. Second factor challenge is required.
let resolver = authError.userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver
var displayNameString = ""
for tmpFactorInfo in (resolver.hints)
displayNameString += tmpFactorInfo.displayName ?? ""
displayNameString += " "
self.showTextInputPrompt(withMessage: "Select factor to sign in\n\(displayNameString)", completionBlock: userPressedOK, displayName in
var selectedHint: PhoneMultiFactorInfo?
for tmpFactorInfo in resolver.hints
if (displayName == tmpFactorInfo.displayName)
selectedHint = tmpFactorInfo as? PhoneMultiFactorInfo
PhoneAuthProvider.provider().verifyPhoneNumber(with: selectedHint!, uiDelegate: nil, multiFactorSession: resolver.session) verificationID, error in
if error != nil
print("Multi factor start sign in failed. Error: \(error.debugDescription)")
else
self.showTextInputPrompt(withMessage: "Verification code for \(selectedHint?.displayName ?? "")", completionBlock: userPressedOK, verificationCode in
let credential: PhoneAuthCredential? = PhoneAuthProvider.provider().credential(withVerificationID: verificationID!, verificationCode: verificationCode!)
let assertion: MultiFactorAssertion? = PhoneMultiFactorGenerator.assertion(with: credential!)
resolver.resolveSignIn(with: assertion!) authResult, error in
if error != nil
print("Multi factor finanlize sign in failed. Error: \(error.debugDescription)")
else
self.navigationController?.popViewController(animated: true)
)
)
else
print(error.localizedDescription)
return
// ...
return
// User is signed in
// ...
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!)
let firebaseAuth = Auth.auth()
do
try firebaseAuth.signOut()
catch let signOutError as NSError
print ("Error signing out: %@", signOutError)
【问题讨论】:
你在哪里定义isMFAEnabled
?
你在那里使用它,但你根本没有声明变量。这就是该消息试图告诉您的内容。
你解决了吗,我也卡住了
所以真正的问题是我们在哪里可以找到完整的示例源代码...
【参考方案1】:
因此,样板代码比您需要的要多得多。这是我对您的 AppDelegate 的评论。
didFinishLaunchingWithOptions 看起来不错。
专业提示:添加此行以保持登录状态GIDSignIn.sharedInstance()?.restorePreviousSignIn()
open,options 看起来不错。
didSignInFor user 是纠结的地方。删除整个函数并将其替换为以下扩展名(在类的括号外):
(同时删除类协议中的GIDSignInDelegate
)
extension AppDelegate: GIDSignInDelegate
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?)
//handle sign-in errors
if let error = error
if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue
print("The user has not signed in before or they have since signed out.")
else
print("error signing into Google \(error.localizedDescription)")
return
// Get credential object using Google ID token and Google access token
guard let authentication = user.authentication else return
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
// Authenticate with Firebase using the credential object
Auth.auth().signIn(with: credential) (authResult, error) in
if let error = error
print("authentication error \(error.localizedDescription)")
我今天对此进行了测试,它目前正在运行 (Swift 5/ios 13.6)。
【讨论】:
【参考方案2】:TL;DR;如果您不想启用多重身份验证,则可以删除该变量。
Firebase 文档提供了该变量,以供您启用/禁用多因素 (MF) 身份验证(即当 facebook 向您发送短信以供您验证时)。更像是他们给了你一个不完整的模板,除非你声明和设置这个变量(以及其他东西,比如实现showTextInputPrompt
),否则它不会编译。
firebase 文档中提供的代码是一个示例,所以不要指望它开箱即用。
【讨论】:
等等...为什么要删除isMFAEnabled
?有什么重要性?
是的,它是 Firebase 提供的示例的一部分。它指示您是否要启用多因素身份验证。如果您希望它始终启用,您可以简单地忽略它
谢谢!更新了您的答案并进行了一些澄清。以上是关于Firebase google 登录身份验证 AppDelegate-使用未解析的标识符“isMFAEnabled”的主要内容,如果未能解决你的问题,请参考以下文章
使用firebase google身份验证单击登录按钮时总是会自动登录,而没有选择以其他用户身份登录的选项
Firebase Google 身份验证,退出并再次登录将使用上次登录的帐户登录
Flutter Firebase - 未能正确删除经过 Google 身份验证的用户