3

我在实现 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);   
  }
}
4

3 回答 3

0

这对我有用

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (await _isBiometricAvailable()) {
    await _getListOfBiometricTypes();
    await _authenticateUser();
  }

  runApp(App());
}
于 2020-08-24T23:48:47.627 回答
0

我想我设法解决了它。如果还有其他问题,请告诉我。

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  bool isAuthenticated = await Authentication.authenticateWithBiometrics();

  if (isAuthenticated) {
   runApp(MyApp());
 } else {
   main();
 }
}

每次我打开应用程序时,这似乎都有效。此外,这是我的 authentification.dart 文件

class Authentication {
  static Future<bool> authenticateWithBiometrics() async {
   final LocalAuthentication localAuthentication = LocalAuthentication();
   bool isBiometricSupported = await localAuthentication.isDeviceSupported();
   bool canCheckBiometrics = await localAuthentication.canCheckBiometrics;

   bool isAuthenticated = false;

   if (isBiometricSupported && canCheckBiometrics) {
     isAuthenticated = await localAuthentication.authenticate(
       localizedReason: 'Please complete the biometrics to proceed.',
       //biometricOnly: true,
       stickyAuth: true,
     );
   }

   return isAuthenticated;
 }
}
于 2021-07-16T15:40:16.917 回答
-1

免责声明:我在 iOS 开发方面没有太多经验,所以我在这里从 Android 进行推断。

我认为问题在于系统对话框使您的应用程序处于非活动状态,从而导致无限循环

  1. 应用程序恢复
  2. 该应用程序显示一个 TouchID/FaceID 对话框,从而使其自身处于非活动状态
  3. 用户确认对话框
  4. 您的应用程序再次进入前台,从而恢复
  5. 见步骤 1

可能的解决方案

  • 不要在应用程序启动时要求身份验证,而是在应用程序中即将发生重要操作时要求身份验证。这是要使用身份验证功能的方式,因此它是最惯用的解决方案。(我最喜欢的)
  • 设置时间限制,例如仅在用户离开超过 x 秒时才显示对话框,从而从短阶段(包括用于身份验证的阶段)中过滤掉较长的非活动阶段。(对我来说感觉像是一种解决方法)
于 2018-10-31T07:25:33.937 回答