首页 > 解决方案 > Firebase 电话身份验证 (Flutter) 在某些 iOS 设备中不起作用

问题描述

我已经使用 firebase 电话身份验证在颤振应用程序中实现了电话号码身份验证。它在 Android 中运行良好。但它在 iOS 中无法正常工作,因为许多用户在提交短信验证码后都面临错误,尽管很多其他人都在使用该应用程序很好。这种情况的可能原因是什么?我已经在下面提交了我的代码。

提交号码

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

代码提交

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

使用的插件

google_sign_in: ^4.0.1+3

firebase_auth:^0.11.0

标签: androidiosdartflutter

解决方案


尝试将 REVERSE_CLIENT_ID 自定义 URL 方案添加到您的 Xcode 项目。

根据火力基地文件:

iOS 设置注意事项:应用验证可能会使用 APN,如果使用模拟器(其中 APN 不起作用)或未在您使用的设备上设置 APN,则必须将 URL 方案设置为 GoogleServices-Info.plist 文件中的 REVERSE_CLIENT_ID。

如何将自定义 URL 方案添加到您的 Xcode 项目:

  1. 打开您的项目配置:双击左侧树视图中的项目名称。从 TARGETS 部分中选择您的应用程序,然后选择 Info 选项卡,然后展开 URL Types 部分。
  2. 单击 + 按钮,并为您的反向客户端 ID 添加 URL 方案。要查找此值,请打开 GoogleService-Info.plist 配置文件,然后查找 REVERSED_CLIENT_ID 键。复制该键的值,并将其粘贴到配置页面上的 URL 方案框中。将其他字段留空。

来自这里的参考资料:

https://pub.dev/packages/firebase_auth

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


推荐阅读