【问题标题】:Flutter - Prompt for TouchID/FaceID when opening appFlutter - 打开应用程序时提示输入 TouchID/FaceID
【发布时间】:2019-04-03 19:37:15
【问题描述】:

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

【问题讨论】:

  • 嘿,马特。你解决了吗?

标签: dart flutter touch-id face-id


【解决方案1】:

这对我有用

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

  runApp(App());
}

【讨论】:

  • 这不起作用,因为它只处理应用程序第一次启动的情况,而不是从后台恢复。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

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

    可能的解决方案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2020-12-06
      • 2017-03-05
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多