简体   繁体   中英

Firebase Phone Auth (Flutter) is not working in some iOS devices

I have implemented phone number authentication in a flutter app using firebase phone auth. It is working fine in Android. But it is not working properly in iOS as many users are facing error after they submit sms verification code, though a lot others are using the app just fine. What can be the possible reasons for this scenario? I have submitted my code below.

Number Submission

void _verifyPhoneNumber() async {

final PhoneVerificationCompleted verificationCompleted =
    (AuthCredential phoneAuthCredential) async {

  final FirebaseUser user =
      await _auth.signInWithCredential(phoneAuthCredential);

  if (user != null) {
    phone = user.phoneNumber;
    fid = user.uid;
    saveLogin(context);
  } else {
    _showErrorDialog("User Verification Error!");
  }
};

final PhoneVerificationFailed verificationFailed =
    (AuthException authException) {
  _showErrorDialog("Phone Verification Failed");
};

final PhoneCodeSent codeSent =
    (String verificationId, [int forceResendingToken]) async {
  _verificationId = verificationId;
  setState(() {
    _title = "Verify SMS Code";
    phoneInput = false;
    phoneSubmit = false;
    codeInput = true;
    codeSubmit = true;
  });
};

final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
    (String verificationId) async {
  _verificationId = verificationId;
  setState(() {
    _title = "Verify SMS Code";
    phoneInput = false;
    phoneSubmit = false;
    codeInput = true;
    codeSubmit = true;
  });
};

await _auth.verifyPhoneNumber(
    phoneNumber: "+880" + _mobileNumber,
    timeout: const Duration(seconds: 5),
    verificationCompleted: verificationCompleted,
    verificationFailed: verificationFailed,
    codeSent: codeSent,
    codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }

Code Submission

void _signInWithPhoneNumber(String _code) async {

final AuthCredential credential = PhoneAuthProvider.getCredential(
  verificationId: _verificationId,
  smsCode: _code,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
if (user != null) {
  phone = user.phoneNumber;
  fid = user.uid;
  saveLogin(context);
} else {
  _showErrorDialog("User Verification Error!");
}
  }

Plugins Used

google_sign_in: ^4.0.1+3

firebase_auth: ^0.11.0

Try adding the REVERSE_CLIENT_ID custom URL schemes to your Xcode project.

According to the firebase documentation:

iOS setup note: App verification may use APNs, if using a simulator (where APNs does not work) or APNs is not setup on the device you are using you must set the URL Schemes to the REVERSE_CLIENT_ID from the GoogleServices-Info.plist file.

How to add custom URL schemes to your Xcode project:

  1. Open your project configuration: double-click the project name in the left tree view. Select your app from the TARGETS section, then select the Info tab, and expand the URL Types section.
  2. Click the + button, and add a URL scheme for your reversed client ID. To find this value, open the GoogleService-Info.plist configuration file, and look for the REVERSED_CLIENT_ID key. Copy the value of that key, and paste it into the URL Schemes box on the configuration page. Leave the other fields blank.

References from here:

https://pub.dev/packages/firebase_auth

https://firebase.google.com/docs/auth/ios/phone-auth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM