So I am trying to create a phone authentication for our iOS app with swift/swfitUI and Firebase. I followed all the steps provided by Firebase:
- enabled the Phone Number sign-in for our Firebase Project
- enabled push notifications inside our app in XCode
- created and APN key for our app, uploaded it to Firebase with team ID and key ID
- setup the reCAPTCHA in XCode
- installed firebase-ios-sdk
and added this code (a function that holds the phoneAuth.verifyPhoneNumber function):
import Firebase import FirebaseAuth import FirebaseCore func checkPhoneNumber(phoneNumber: String, completion: @escaping VerificationCompletion) { let phoneAuth = PhoneAuthProvider.provider() print(phoneAuth) print(phoneNumber) FirebaseConfiguration.shared.setLoggerLevel(.debug) phoneAuth.verifyPhoneNumber("+1"+phoneNumber, uiDelegate: nil) { verificationID, error in if let verificationID = verificationID { print(verificationID) } if let error = error { print(error.localizedDescription) completion(false, .failure(error)) return } // Store the verification ID for later use UserDefaults.standard.set(verificationID, forKey: "verificationID") completion(true,.success(verificationID ?? "")) } print("Function checkPhoneNumber run.") } this is how I call it:
Button(action: { let impactLight = UIImpactFeedbackGenerator(style: .light) impactLight.impactOccurred() checkPhoneNumber(phoneNumber: user.phoneNumber) { success, error in print(success, error) if success { DispatchQueue.main.async { router.push(.page3) } } else { print("error!") } } }, label: { continueButton() .padding() }) Each time I press the button I get only this (function .verifyPhoneNumber not getting called): <FIRPhoneAuthProvider: 0x600003db33e0> (453) 454-33 Function checkPhoneNumber run.
Tried changing the number format. Tried taking the .verifyPhoneNumber out of checkPhoneNumber function, so it is called directly with the button click. I updated the firebase-ios-sdk. I deleted the previous APN key and created a new one. I tried running the app on my iPhone in hopes of a different result. Tried deleting google-info.plst, downloading it again and putting inside XCode. Tried enabling background modes on the simulator. All without success. The problem is that I cannot get any errors, because the function is not working at all.
Tried this enabling this also: Auth.auth().settings.isAppVerificationDisabledForTesting = TRUE
phoneAuthis getting released as soon ascheckPhoneNumberfinishes execution. You need to store it somewhere, e.g. in your view model, or use async call variant, that should preventcheckPhoneNumberfrom finishing until you get the resultprivate var phoneAuth: PhoneAuthProvider?and the functioncheckPhoneNumberand then calling the class with the function. It didn't work. I also tried usingDispatchQueue.global().async, it didn't work. Maybe I implemented in a wrong way... Any implementaion suggestions or any other solution suggestions that might solve the problem?