我在实现 TouchID/FaceID 身份验证时遇到问题,当用户打开应用程序时它会自动提示用户。我正在为 TouchID/FaceID 使用 local_auth 依赖项。
在下面的代码中,应用程序恢复时会弹出生物认证,但也无法关闭。如果您按下主页按钮,它会关闭 TouchID 提示,但会立即开始重试,如果您继续尝试,则会导致无限循环。它还会随机提示两次,所以即使你用第一个 TouchID 提示登录成功,之后它也会立即再次弹出。有谁知道解决这个问题的方法?我在登录页面上还有一个 TouchID 按钮,用户可以按下该按钮来手动提示 TouchID,但我很想重新创建我的银行应用程序和其他应用程序在您自动打开应用程序时提示 TouchID 的工作方式。
void initState() {
super.initState();
SystemChannels.lifecycle.setMessageHandler((msg) async {
if (msg==AppLifecycleState.resumed.toString()) {
// If can pop, app is not at login screen
// so don't prompt for authentication
if (!Navigator.canPop(context)) {
if (_useFingerprint) {
SharedPreferences prefs = await SharedPreferences.getInstance();
String password = prefs.getString('password');
biometricAuthentication(_username, password);
}
}
}
});
void biometricAuthentication(String user, String pass) async {
print("Biometric Authentication Called");
bool didAuthenticate = false;
try {
didAuthenticate = await localAuth.authenticateWithBiometrics(localizedReason: 'Please authenticate');
} on PlatformException catch (e) {
print(e);
}
if (!mounted) {
return;
}
if (!didAuthenticate) {
return;
}
else {
normalLogin(user, pass);
}
}