Gle*_*ow 7 ios appdelegate firebase-authentication google-signin swift4
我是 iOS 开发的新手。我正在尝试将 google 登录添加到我的应用程序,但我遇到了一些问题。代码显示了一些“使用未解析的标识符‘isMFAEnabled”和“类型‘AppDelegate’的值没有成员‘showTextInputPrompt’”。请帮助我。我正在关注此文档 - https://firebase.google.com/docs/auth/ios/google-signin#swift_9 在此处输入图片说明
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)
}
Run Code Online (Sandbox Code Playgroud)
因此,样板代码比您需要的代码多得多。这是我对您的 AppDelegate 的评论。
didFinishLaunchingWithOptions看起来不错。专业提示:添加此行以保持登录状态GIDSignIn.sharedInstance()?.restorePreviousSignIn()
打开,选项按原样看起来不错。
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)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我今天对此进行了测试,目前正在运行(Swift 5/ios 13.6)。
TL;博士; 如果您不想启用多重身份验证,则可以删除该变量。
Firebase 文档提供了该变量作为您启用/禁用多重 (MF) 身份验证的方式(即当 facebook 向您发送短信供您验证时)。这更像是他们给你一个不完整的模板,除非你声明并设置这个变量(以及其他诸如实现showTextInputPrompt),否则它不会编译。
firebase 文档中提供的代码只是一个示例,因此不要指望它可以开箱即用。
| 归档时间: |
|
| 查看次数: |
1500 次 |
| 最近记录: |