flutter [local_auth] 文档如何通过本地化FallbackTitle来消除iOS生物识别对话框中的回退按钮,

5t7ly7z5  于 4个月前  发布在  Flutter
关注(0)|答案(4)|浏览(70)

重现步骤

我使用 AuthenticationOptions(biometricOnly: true) 调用本地认证对话框。
当在 iOS 上调用它时,会显示 FaceID 对话框。
如果人脸识别失败两次,它会显示弹出框:

预期结果

如果将 biometricOnly 设置为 true,则不应显示 "Enter password" 选项。

实际结果

点击 "Enter password" 导致异常:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(NotAvailable, Authentication canceled., com.apple.LocalAuthentication, null)

代码示例

本地认证代码示例

final auth = LocalAuthentication();
final result = await auth.authenticate(
  localizedReason: 'Authenticate to access the app',
  options: const AuthenticationOptions(biometricOnly: true),
);

Flutter Doctor 输出

医生输出

[✓] Flutter (Channel stable, 3.19.6, on macOS 14.4.1 23E224 darwin-arm64, locale en-PL)
• Flutter version 3.19.6 on channel stable at /Users/xxx/git/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 54e66469a9 (5 weeks ago), 2024-04-17 13:08:03 -0700
• Engine revision c4cd48e186
• Dart version 3.3.4
• DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/xxx/Library/Android/sdk
• Platform android-34, build-tools 34.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15C500b
• CocoaPods version 1.15.2

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.89.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension can be installed from:
🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (5 available)
• sdk gphone64 arm64 (mobile)          • emulator-5554                        • android-arm64  • Android 13 (API 33) (emulator)
• iPhone 15 (mobile)                   • 101FE7E4-58F6-4DA3-92C5-B2F838BAE888 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-2 (simulator)
• macOS (desktop)                      • macos                                • darwin-arm64   • macOS 14.4.1 23E224 darwin-arm64
• Chrome (web)                         • chrome                               • web-javascript • Google Chrome 124.0.6367.210

[✓] Network resources
• All expected network resources are available.

• No issues found!
1szpjjfi

1szpjjfi1#

要隐藏该选项,请将 localizedFallbackTitle 设置为空字符串,根据 Apple docs
根据需要调整标题以清楚地记录这一点(可能在 IOSAuthMessages 上)。

dy1byipe

dy1byipe2#

(从长远来看,我们应该考虑通过处理 userFallback 错误返回来实现管道,而不是将其作为异常处理,但这需要对插件的 API 表面进行更大的更改。)

d4so4syb

d4so4syb3#

感谢您的更新 - 它按照您的描述工作。
然而,我认为在接口层面阻止它是有好处的,原因如下:

  • 我可以通过提供适当的配置来使应用程序以意想不到的方式行为。
  • 我需要直接依赖于未使用的库,以防止不必要的行为。

我认为对LocalAuthentication.authentication方法的简单修复可能会有所帮助。

final iOSAuthMessages =
    (authMessages.firstOrNullWhereType<IOSAuthMessages>() ?? IOSAuthMessages()).copyWith(
  localizedFallbackTitle: '',
);
authMessages = options.biometricOnly
    ? [
        ...authMessages.whereNotType<IOSAuthMessages>(),
        iOSAuthMessages,
      ]
    : authMessages;

return LocalAuthPlatform.instance.authenticate(
  localizedReason: localizedReason,
  authMessages: authMessages,
  options: options,
);
aelbi1ox

aelbi1ox4#

如果你愿意,我们可以在这里接受一个拉取请求!如果你需要帮助,请告诉我们:)

相关问题