Flutter InAppWebView调用iOS生物识别遇AuthorizationError Code=1004问题
解决Flutter InAppWebView中Web生物识别认证的Code=1004错误
你遇到的ASAuthorizationController credential request failed with error: Error Domain=com.apple.AuthenticationServices.AuthorizationError Code=1004错误,通常是WebView未正确配置Web Authentication API(WebAuthn)支持,或者权限配置不全导致的。以下是针对性的解决步骤:
1. 完善Info.plist配置
确保Info.plist中同时添加以下两个权限描述(覆盖Face ID/Touch ID场景):
<key>NSFaceIDUsageDescription</key> <string>用于Web平台的生物识别认证</string> <key>NSBiometricUsageDescription</key> <string>用于Web平台的生物识别认证</string>
注意:需确认所有Build Target(Debug/Release/Profile)都包含这些配置,避免仅在单个Target中添加导致权限不生效。
2. 启用InAppWebView的WebAuthn支持
在你的InAppWebViewSettings中添加isWebAuthenticationSessionEnabled配置,这是Web生物识别认证的核心开关:
webViewSettings: InAppWebViewSettings( // 原有配置保留... isWebAuthenticationSessionEnabled: true, // 新增此行开启WebAuthn支持 userAgent: Platform.isAndroid ? "Mozilla/5.0 (Linux; Android 14) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.6367.82 Mobile Safari/537.36" : "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Mobile/15E148 Safari/604.1", cacheEnabled: true, javaScriptEnabled: true, isElementFullscreenEnabled: true, ),
3. 处理WebAuthenticationSession请求回调
在MyInAppBrowser类中重写onWebAuthenticationSessionRequest方法,主动允许WebAuthn请求:
class MyInAppBrowser extends InAppBrowser { final VoidCallback onBrowserExit; MyInAppBrowser({required this.onBrowserExit}); @override void onExit() { super.onExit(); print("Browser closed!"); onBrowserExit(); } // 新增:处理Web生物识别认证请求 @override void onWebAuthenticationSessionRequest(WebAuthenticationSessionRequest request) async { // 可根据需求添加域名校验逻辑,此处默认允许所有请求 await request.allow(); } }
4. 验证Xcode配置细节
- 确认
Signing & Capabilities中已添加Sign In with Apple(部分WebAuthn实现依赖此Capability); - 检查项目的
Info.plist是否被正确关联到所有Target,避免权限描述未被打包进应用。
完成以上配置后,重新编译运行应用,Web平台的生物识别认证应该能正常触发权限请求并完成认证流程。
内容的提问来源于stack exchange,提问作者Sebastián Mahuzier
相关产品推荐
相关产品推荐

