Flutter Firebase iOS端Apple Sign In无法获取邮箱/名称求助
iOS端Firebase Auth结合AppleAuthProvider无法获取用户邮箱/名称问题
环境信息
- Flutter: 3.22.2
- firebase_core: ^3.1.1
- firebase_auth: ^5.1.1
- Firebase SDK: 10.27.0
- iOS Simulator: iOS 17.5
相关代码
Repository类
static Future<UserCredential> signInWithApple() async { final appleProvider = AppleAuthProvider(); appleProvider.addScope('email'); appleProvider.addScope('name'); return await FirebaseAuth.instance.signInWithProvider(appleProvider); }
Controller类
Future<void> appleSignIn() async { try { final credential = await AuthenticationRepository.signInWithApple(); print(credential.additionalUserInfo); } on FirebaseAuthException catch (e) { return Future.error(e); } }
问题详情
已按照Flutter Firebase联合身份验证文档操作,但登录时仅显示基础弹窗“是否使用您的Apple ID xxx@xx.com登录APP_NAME?”,无法获取用户邮箱或名称。
首次登录时未添加email权限,之后尝试过以下操作但均无效:
- 在Firebase Authentication中删除邮箱为空的用户
- 在iOS设置的“密码与安全性”->“使用Apple ID登录”中停止应用的Apple ID授权
- 执行
flutter clean && flutter pub get,进入ios目录删除Podfile.lock并执行pod install --repo-update - 删除应用后重新安装测试
解决方案
补充Info.plist权限描述
Apple要求权限请求必须附带用途说明,在iOS项目的Info.plist中添加以下内容:<key>NSUserEmailUsageDescription</key> <string>需要获取您的邮箱以完成账号注册与验证</string> <key>NSUserNameUsageDescription</key> <string>需要获取您的名称以完善账号信息</string>强制触发权限请求弹窗
Apple登录的权限弹窗仅在首次授权时显示,即使取消应用授权,系统也可能不再弹出。可尝试:- 使用全新的Apple ID进行测试,新ID首次登录会触发完整的权限请求弹窗
- 在iOS设置中进入“隐私与安全性”->“Apple ID登录”,找到对应应用后选择“编辑共享信息”,手动开启邮箱和名称的共享权限
调整用户信息提取逻辑
不要仅依赖additionalUserInfo,直接从User对象中提取信息,修改Controller代码:Future<void> appleSignIn() async { try { final credential = await AuthenticationRepository.signInWithApple(); final user = credential.user; print('邮箱: ${user.email}'); print('名称: ${user.displayName}'); print('用户资料: ${credential.additionalUserInfo?.profile}'); } on FirebaseAuthException catch (e) { return Future.error(e); } }验证Apple Developer配置
登录Apple Developer后台检查:- 应用已启用“Sign in with Apple”功能
- 服务ID、返回URL与Firebase控制台中的配置完全匹配
- App ID已勾选“Sign in with Apple”权限
清除系统授权缓存
除了取消应用授权,还可尝试:- 重启iOS设备
- 退出当前Apple ID并重新登录后,再进行应用授权测试
内容的提问来源于stack exchange,提问作者Zeddy
相关产品推荐
相关产品推荐

