SwiftUI中GameKit点击登录图标弹窗闪退问题求助
GameCenter 点击图标界面闪显即关闭问题的解决方案
问题背景
SwiftUI主界面中通过代码实现的GameCenter登录按钮、通知及登录后可点击图标功能,此前运行正常,近期出现异常:点击已登录的GameCenter图标时,GameKit界面仅闪显瞬间就自动关闭。
实现代码
主界面生命周期代码
.onAppear { GKAccessPoint.shared.location = .topLeading authenticateUser() GKAccessPoint.shared.isActive = true }
用户认证函数
func authenticateUser() { localPlayer.authenticateHandler = { vc, error in guard error == nil else { print(error?.localizedDescription ?? "") return } } }
触发的错误日志
点击图标时控制台输出以下错误信息:
Cannot add page fields because none are specified by the metrics fields context. Did you forget to add an PageMetricsPresenter to the object graph used for actions? Error acquiring assertion: <Error Domain=RBSAssertionErrorDomainCode=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}> [com.apple.GameCenterUI.GameCenterDashboardExtension(1.0)] Connection to plugin interrupted while in use.
可行解决方案
- 调整GKAccessPoint激活时机:不要在
onAppear中直接激活,改为在认证完成后再启用。修改认证函数及onAppear代码:// 修改后的认证函数 func authenticateUser() { localPlayer.authenticateHandler = { [weak self] vc, error in guard error == nil else { print(error?.localizedDescription ?? "") return } DispatchQueue.main.async { GKAccessPoint.shared.isActive = true } } } // 修改后的onAppear .onAppear { GKAccessPoint.shared.location = .topLeading authenticateUser() } - 核对项目配置:检查
Info.plist是否包含NSGameCenterUsageDescription字段;登录Apple开发者后台确认App ID已开启GameCenter服务,对应Provisioning Profile权限齐全。 - 清理运行环境:删除设备/模拟器上的App,执行Xcode清理(
Cmd+Shift+K)后重新编译;重启测试设备或模拟器,确保GameCenter进程状态正常。 - 适配系统版本:若为iOS版本升级后出现问题,检查
GKAccessPoint相关API是否有变更,避免在非主线程操作GameKit的UI组件。
内容的提问来源于stack exchange,提问作者Daniel Klöck
相关产品推荐
相关产品推荐

