在我的 iOS 回合制比赛中,我尝试接收通知并获取
public func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool)
被调用,但没有成功。
我将 View 模型注册到本地播放器
GKLocalPlayer.localPlayer().register(self)
我希望在其他玩家执行后触发
func endTurn(withNextParticipants nextParticipants: [GKTurnBasedParticipant], turnTimeout timeout: TimeInterval, match matchData: Data, completionHandler: ((Error?) -> Swift.Void)? = nil)
但没有成功。
如果我强制重新加载 matchData,那么我将获得第二个玩家刚刚提交的数据。所以 endTurn 可以正常工作。
我是不是做错了什么?
更新: 所以我创建一个新项目,复制所有文件, 在功能中仅启用了游戏中心。
当开发它工作完美时,我连接了两个设备(具有不同的Apple ID)。通知正在运行,并且 Turnbasedlistener 正在触发。
当我发布它进行内部测试时,它就停止工作了!
最佳答案
我有非常类似的问题。我的解决方案是在等待轮到我时手动重新检查我的状态。
首先,我定义了全局变量 var gcBugTimer: Timer
在 endTurn(withNextParticipants:turnTimeOut:match:completionHandler:)
完成处理程序中:
let interval = 5.0
self.gcBugTimer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(self.isMatchActive), userInfo: nil, repeats: true)
self.gcBugTimer.tolerance = 1.0
如果一个玩家正在为新的比赛而高兴,而另一个玩家正在轮流中,也应该调用上面的代码。
然后定时器方法:
func isMatchActive() {
// currentMatch - global variable contains information about current match
GKTurnBasedMatch.load(withID: currentMatch.matchID!) { (match, error) in
if match != nil {
let participant = match?.currentParticipant
let localPlayer = GKLocalPlayer.localPlayer()
if localPlayer.playerID == participant?.player?.playerID {
self.player(localPlayer, receivedTurnEventFor: match!, didBecomeActive: false)
}
} else {
print(error?.localizedDescription ?? "")
}
}
}
我在 player(_:receivedTurnEventFor:didBecomeActive)
的开头添加以下代码:
if gcBugTimer != nil && gcBugTimer.isValid {
gcBugTimer.invalidate()
}
关于iOS 回合制匹配,推送通知不起作用,GKTurnBasedEventListener 函数未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44609360/