11

I'm new to iOS development. I am trying to add google sign in to my app but i am facing an some problems.Code shows some "Use of unresolved identifier 'isMFAEnabled"and "Value of type 'AppDelegate' has no member 'showTextInputPrompt'".Please help me.I'm following this doc- https://firebase.google.com/docs/auth/ios/google-signin#swift_9 enter 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) } 
7
  • Where did you define isMFAEnabled? Commented Jun 28, 2020 at 15:08
  • You're using it there, but you didn't declare the variable at all. That's what the message is trying to tell you. Commented Jun 28, 2020 at 15:32
  • Did you solve this, I'm also stuck Commented Jul 12, 2020 at 17:53
  • So the real question is rather where can we find the full sample source code... Commented Dec 8, 2020 at 19:30
  • I have to say their code is a mess I literally took my hands to my head when organizing their code Commented Mar 30, 2022 at 23:18

2 Answers 2

6

So the boilerplate is wayyyy more code than you need. This is my review of your AppDelegate.

  1. didFinishLaunchingWithOptions looks good as-is. Pro tip: add this line for staying logged in GIDSignIn.sharedInstance()?.restorePreviousSignIn()

  2. open,options looks good as-is.

  3. didSignInFor user is where it gets tangled. Delete the entire function and replace it with the following extension (outside the class' brackets):

(also delete GIDSignInDelegate in the class protocols)

 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)") } } } } 

I have tested this in today and it's currently working (Swift 5/ios 13.6).

Sign up to request clarification or add additional context in comments.

Comments

5

TL;DR; you can just delete that variable if you don't want to enable multi-factor authentication.

The Firebase documentation provides the variable as a way for you to enable/disable multi-factor (MF) authentication (i.e. when facebook sends you a text message for you to verify). It's more like they are giving you an incomplete template that doesn't compile unless you declare and set this variable (among other things like implementing showTextInputPrompt).

The code provided in firebase documentation is an example, so don't expect it to work out of the box.

3 Comments

Wait... why should you remove isMFAEnabled? What is the importance?
Yeah it's part of the example Firebase provided. It's indicating whether you would want to enable multi-factor auth enabled or not. You can simply ignore it if you want it to be enabled always
Thanks! Updated your answer with some clarification.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.