问题描述
我已经使用Firebase电话身份验证在flutter应用程序中实现了电话号码身份验证.在Android中运行正常.但这在iOS中无法正常运行,因为许多用户在提交短信验证码后都遇到了错误,尽管许多其他人都很好地使用了该应用.这种情况可能是什么原因?我已经在下面提交了我的代码.
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.
提交号码
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!");
}
}
使用的插件
firebase_auth:^ 0.11.0
firebase_auth: ^0.11.0
推荐答案
尝试将REVERSE_CLIENT_ID自定义URL方案添加到您的Xcode项目中.
Try adding the REVERSE_CLIENT_ID custom URL schemes to your Xcode project.
根据firebase文档:
According to the firebase documentation:
iOS设置说明:如果您正在使用的设备上未使用模拟器(其中APN不起作用)或未设置APN,则应用程序验证可能会使用APN,您必须将URL方案设置为GoogleServices-Info的REVERSE_CLIENT_ID. plist文件.
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.
如何在Xcode项目中添加自定义URL方案:
How to add custom URL schemes to your Xcode project:
- 打开项目配置:在左树视图中双击项目名称.从目标"部分中选择您的应用,然后选择信息"选项卡,然后展开"URL类型"部分.
- 单击+按钮,然后为您的反向客户ID添加URL方案.要找到此值,请打开GoogleService-Info.plist配置文件,然后查找REVERSED_CLIENT_ID密钥.复制该键的值,然后将其粘贴到配置页上的"URL方案"框中.将其他字段留空.
此处引用:
https://pub.dev/packages/firebase_auth
https://firebase.google.com/docs/auth/ios /phone-auth
这篇关于Firebase电话身份验证(Flutter)在某些iOS设备中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!