我正在尝试将 Game Center 集成到 watchOS 应用程序上,但无法验证本地玩家的身份。
也许我缺少任何步骤来允许配套的 watchOS 应用程序像 iOS 应用程序一样访问游戏中心?
在我正在调用的 watchOS 应用程序扩展上:
GKLocalPlayer.local.authenticateHandler = { error in
}
这与 iOS 上用于身份验证的方法非常相似,并且在 iOS 上它可以工作,甚至在模拟器上也是如此。
当调用该方法并在模拟器上运行时,我在 Xcode 的调试区域上收到此日志:
[Error] _authenticateUsingAlert:Failed to authenticate player with existing credentials.Error: Error Domain=GKErrorDomain Code=6 "The requested operation could not be completed because local player has not been authenticated." UserInfo={NSLocalizedDescription=The requested operation could not be completed because local player has not been authenticated.}
完成处理程序收到的错误消息是:
Error Domain=GKErrorDomain Code=3 "The requested operation could not be completed due to an error communicating with the server." UserInfo={NSLocalizedDescription=The requested operation could not be completed due to an error communicating with the server.}
在真实设备上测试相同的场景时,我得到日志:
[Error] _authenticateUsingAlert:Failed to authenticate player with existing credentials.Error: Error Domain=GKErrorDomain Code=15 "The requested operation could not be completed because this application is not recognised by Game Center." UserInfo={GKServerStatusCode=5019, NSUnderlyingError=0x11e3a6c0 {Error Domain=GKServerErrorDomain Code=5019 "status = 5019, no game matching descriptor: ios:com.myApp.test.watch.extension::0+-1" UserInfo={GKServerStatusCode=5019, NSLocalizedFailureReason=status = 5019, no game matching descriptor: ios:com.myApp.test.watch.extension::0+-1}}, NSLocalizedDescription=The requested operation could not be completed because this application is not recognised by Game Center.}
来自完成处理程序的错误是:
Error Domain=GKErrorDomain Code=15 "The requested operation could not be completed because this application is not recognised by Game Center." UserInfo={NSLocalizedDescription=The requested operation could not be completed because this application is not recognised by Game Center.}
这两个错误与当应用程序未在 App Store Connect 上启用 Game Center 时您在 iOS 上收到的消息类似。正如之前提到的,情况并非如此,因为在 iOS 上工作正常。
我还尝试将应用程序移至 App Store Connect 上的游戏中心组,因为它说这是为了共享排行榜和成就,但结果是相同的。
经过数周的揪心,我发现(艰难的方式)游戏中心只能在真正的设备上在 watchOS 上运行,而且它的运行非常有限。 您可以访问本地玩家显示名称,但无法提交分数。
我什至尝试将应用程序添加到游戏中心组,但没有帮助。
--
PS。它仍然会在 Xcode 控制台上显示错误消息和警告,指出用户未经过身份验证或 Game Center 无法识别该应用程序,但如果您在使用
GKLocalPlayer.local.displayName
进行身份验证后尝试获取
GKLocalPlayer.local.authenticateHandler
,它将起作用。显然它也会将分数提交到排行榜,或者至少不会显示错误,但分数永远不会被提交。
GKLocalPlayer.local.authenticateHandler
在watchOS上向游戏中心进行身份验证似乎无法正常工作,因此无法直接从手表应用程序报告分数,或者至少即使在具有具有工作游戏中心排行榜的实时应用程序(在 ios 上工作)。
作为另一种解决方案,您可以在本地 watchOS 上保存分数,例如保存到UserDefaults
,然后将它们传输到配套的 iOS 应用程序(如果有)。为了传输该数据,您可以使用
WatchConnectivity
框架,该框架提供带有 WCSession
方法的 updateApplicationContext
类,您可以在手表设备上使用该类,并在 iOS 设备上使用 func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any])
委托方法接收该数据。然后,当您在 ios 应用程序上获得分数时,您可以像往常一样从 ios 应用程序将其发送到游戏中心。不幸的是,这要求用户不时启动 ios 配套应用程序,但最好什么都不启动。