iOS 回合制匹配,推送通知不起作用,GKTurnBasedEventListener 函数未调用

标签 ios swift gamekit gkturnbasedmatch

在我的 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:completionHan‌​dler:) 完成处理程序中:

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/

相关文章:

iphone - 如何通过坐标/位置 (x,y) 查找 UIView

ios - 如果我使用 Apple Push Notification 服务进行即时通讯,苹果会封锁我的帐户吗?

swift - 如何使用 Alamofire 从 URL 下载文件到自定义文件夹

ios - 制作许多类似的 ViewController

ios11游戏中心查找玩家失败

ios - 如何删除 SwiftUI 中列表行的最小垂直填充或最小高度?

ios - iTunes Connect、内部测试、供应商 ID 更改

ios - 当我使用 .ascii 编码解码 HTML 页面时出现奇怪的字符串

iphone - 如何以编程方式检测游戏中心?

ios - GameKit 点对点连接问题。向另一台设备发送邀请需要很长时间