菜鸟教程小白 发表于 2022-12-13 13:27:37

iphone - 进入后台时 Gamekit 继续 session


                                            <p><p>我使用 GameKit 和 GKMatch 在两台 iOS 设备之间建立连接以进行同步游戏。当一名玩家进入后台时,连接将断开。我怎样才能避免这种行为?当一位用户确实将应用程序暂停到后台时,是否可以通过 GKMatch 保持连接? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>根据 Apple 的文档:</p>

<blockquote>
<p>As soon as your game moves to the background, the value of the local player object’s authenticated property becomes and remains invalid until your game moves back to the foreground. You cannot read the value to determine if the player is still authenticated until Game Kit reauthenticates the player and calls your authentication handler. Your game must act as though there is not an authenticated player until your completion handler is called. Once your handler is called, value stored in the authenticated property is valid again.</p>
</blockquote>

<p>来源:<a href="https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/GameKit_Guide.pdf" rel="noreferrer noopener nofollow">Game Center Programming Guide, pg. 39</a> .</p>

<p>除非您的应用使用 VOIP 或特殊情况,否则您不应该在后台保持网络连接打开。 (如果您有兴趣,请查看 <a href="https://stackoverflow.com/questions/5840365/how-can-an-ios-app-keep-a-tcp-connection-alive-indefinitely-while-in-the-backgro" rel="noreferrer noopener nofollow">this SO question</a> 以了解黑客式的处理方式)。
由于您的播放器身份验证在进入后台时失效,另一台设备(如果它确实设法从后台应用程序接收网络数据)将不会(可靠地)知道哪个 GKPlayer 发送了信息。</p>

<p>因此,虽然 Apple 没有明确表示,但所有 Game Center 功能通常应在应用程序进入后台时取消,并在重新启动时重新验证玩家身份后恢复。</p>

<p>附带说明,当应用退出后台时(通常,取决于后台任务)被视为“有资格暂停”。这是多任务执行存在此类限制的原因之一 - 您的应用程序可能随时被终止以释放内存,而您真的不希望在发生这种情况时进行繁重的数据传输!
见 <a href="http://developer.apple.com/library/ios/#technotes/tn2277/_index.html" rel="noreferrer noopener nofollow">TN227</a>如果您想了解更多关于网络和多任务处理的信息。</p>

<p><strong>编辑:另一种方法:</strong></p>

<p>虽然无法在应用程序在后台运行时保持 GKMatch 处于事件状态,但模拟此行为的另一种方法是将游戏状态保存到您自己的服务器。要求用户在离开游戏之前与一些 UI 交互(可能按下按钮),这会将当前游戏状态保存到您自己的服务器并通知其他玩家您即将离开游戏。重新启动后,查询您的服务器以查看您是否有任何已保存的游戏,如果有,则加载游戏状态并向其他玩家发送通知。这并不严格维持相同的游戏,而是一种近似您所追求的行为的方法。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 进入后台时 Gamekit 继续 session ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10352761/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10352761/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 进入后台时 Gamekit 继续 session