Unity中Firebase Apple Sign In认证成功但游戏无状态更新问题
Unity Firebase Apple Sign In 无响应问题排查与修复
问题概述
点击Apple Sign In弹窗完成流程后,游戏无任何更新,未触发用户信息同步或页面跳转,Xcode控制台无报错日志。
核心问题与修复方案
1. 未触发登录后的跳转逻辑
你的Checking协程包含了登录成功后的页面切换/完善Profile逻辑,但在Firebase登录成功的回调中从未启动该协程,导致登录完成后没有后续流程。
修复代码:在SignInWithAppleOnFirebase的成功回调末尾添加协程启动代码:
Firebase.Auth.AuthResult result = task.Result; Debug.LogFormat("User signed in successfully: {0} ({1})", result.User.DisplayName, result.User.UserId); FirebaseUser newUser = task.Result.User; DataSaver.instance.SaveUserId(newUser.UserId); loadData.LoadData(newUser.UserId); Debug.LogFormat("User signed in successfully: {0} ({1})", newUser.DisplayName, newUser.UserId); DataSaver.instance.dts.parentName = newUser.DisplayName; DataSaver.instance.dts.mobileNumber = newUser.PhoneNumber; DataSaver.instance.dts.email = newUser.Email; // 新增:启动跳转逻辑协程 StartCoroutine(Checking(newUser.UserId));
2. 异步回调未在Unity主线程执行
Firebase的ContinueWith默认在后台线程执行,而Unity的UI操作、场景切换、协程启动必须在主线程完成,后台线程执行这些操作会静默失败,无报错。
修复代码:使用Firebase提供的ContinueWithOnMainThread替代ContinueWith,确保回调在主线程执行:
// 替换SignInAndRetrieveDataWithCredentialAsync的回调 auth.SignInAndRetrieveDataWithCredentialAsync(credential).ContinueWithOnMainThread(task => { // 原回调逻辑保持不变 }); // 同时修复CheckFirebaseDependencies中的回调 FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => { // 原逻辑保持不变 });
3. AppleAuthManager被意外GC回收
在SignInWithApple方法中创建的AppleAuthManager是局部变量,方法执行完毕后可能被垃圾回收,导致Apple登录的回调无法正常触发。
修复代码:将AppleAuthManager设为类成员变量,避免被回收:
#if UNITY_IOS private AppleAuthManager appleAuthManager; #endif private void Awake() { CheckFirebaseDependencies(); #if UNITY_IOS appleAuthManager = new AppleAuthManager(new PayloadDeserializer()); #endif } public void SignInWithApple() { Debug.Log("Sign In With Apple"); #if UNITY_IOS rawNonce = GenerateNonce(); appleAuthManager.LoginWithAppleId( LoginOptions.IncludeEmail | LoginOptions.IncludeFullName, credential => { // 原回调逻辑保持不变 }, error => { AddToInformation("Apple Sign-In Failed: " + error.ToString()); }); #else AddToInformation("Apple Sign-In is only supported on iOS."); #endif }
4. Nonce编码不符合Firebase要求
Firebase要求Apple Sign In的nonce使用Base64URL编码(替换普通Base64的+、/字符,移除末尾=),普通Base64编码会导致验证静默失败。
修复代码:修改GenerateNonce方法:
private string GenerateNonce() { var nonce = new byte[32]; using (var rng = RandomNumberGenerator.Create()) { rng.GetBytes(nonce); } // 转换为Base64URL格式 return Convert.ToBase64String(nonce) .Replace('+', '-') .Replace('/', '_') .TrimEnd('='); }
额外排查步骤
- 确认Firebase控制台Apple Sign In配置:检查Team ID、Bundle ID、签名证书是否正确,且已启用Apple作为登录提供商。
- 验证Xcode Entitlements:确保
com.apple.developer.applesignin权限已添加,且包含Default作用域。 - 增加日志输出:在
SignInWithAppleOnFirebase回调开头添加Debug.Log("Entering Firebase sign-in callback");,确认回调是否执行;在Checking协程内添加日志,确认流程是否触发。
内容的提问来源于stack exchange,提问作者Muhammed Fasil
相关产品推荐
相关产品推荐

