在游戏中心比赛中出错:被主机取消



我正在开发一个非常简单的游戏来测试Game Center的所有功能,并更加习惯Apple的GameKit和所有这些东西。 这个非常简单的游戏给了我很多问题,我一直在缓慢但肯定地度过难关。我遇到了一个新问题,希望这里有人可以帮助我。
到目前为止,我可以执行以下操作:
-连接到Game Center并验证玩家-
记分板和排行榜&提交分数
-开始匹配过程(但不开始比赛)。
无论如何,这是我"开始比赛"的代码。这是在匹配请求通过并找到 2 名玩家之后:

func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
print("Match found")
if match.expectedPlayerCount == 0 {
viewController.dismiss(animated: true, completion: {self.goToGame(match: match)})
}
func goToGame(match: GKMatch) {
let gameScreenVC = self.storyboard?.instantiateViewController(withIdentifier: "mainGame") as! ViewController
gameScreenVC.providesPresentationContextTransitionStyle = true
gameScreenVC.definesPresentationContext = true
gameScreenVC.modalPresentationStyle = UIModalPresentationStyle.fullScreen
gameScreenVC.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
gameScreenVC.match = match
self.present(gameScreenVC, animated: true, completion: nil)
}

现在,在那里调用的视图控制器的代码具有以下启动代码:

override func viewDidLoad() {
super.viewDidLoad()
yourTurnLabel.isHidden = true
// Do any additional setup after loading the view, typically from a nib.
matchStart()
}
func matchStart() {
let localPlayerID = GKLocalPlayer.localPlayer().playerID
let initialTurn = "initRand,(localPlayerID ?? "ERROR: NO ID"),(aTaps),0,0"
let turnData = initialTurn.data(using: .utf8)
print("Sending String: (initialTurn)")
sendData(turnLog: turnData!)
}
func sendData(turnLog: Data) {
do {
try match?.sendData(toAllPlayers: turnLog, with: GKMatchSendDataMode.reliable)
print("DATA SENT!")
} catch {
print("ERROR: (error.localizedDescription)")
}
}

以及来自 MatchMakingDelegate 的以下扩展:

extension ViewController: GKMatchDelegate {
// The match received data sent from the player.
@available(iOS 8.0, *)
public func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {
print ("RECEIVED DATA 8.0")
print ("Received (data) from (player)")
receiveData(turnLog: data)
}
@available(iOS 9.0, *)
public func match(_ match: GKMatch, didReceive data: Data, forRecipient recipient: GKPlayer, fromRemotePlayer player: GKPlayer) {
print("RECEIVED DATA 9.0")
print ("Received (data) for (recipient) from (player)")
}
// The player state changed (eg. connected or disconnected)
@available(iOS 4.1, *)
public func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {
print("CHANGED STATE")
}

// The match was unable to be established with any players due to an error.
@available(iOS 4.1, *)
public func match(_ match: GKMatch, didFailWithError error: Error?) {
print("FAILED")
}

// This method is called when the match is interrupted; if it returns YES, a new invite will be sent to attempt reconnection. This is supported only for 1v1 games
@available(iOS 8.0, *)
public func match(_ match: GKMatch, shouldReinviteDisconnectedPlayer player: GKPlayer) -> Bool {
return true
}
}

现在,到目前为止,它似乎运行良好,直到我开始游戏,我在 x-Code 控制台中获得以下输出,但我从未看到"接收数据"触发器

AUTHENTICATED!
true
2018-01-23 17:54:01.401426+0700 testGameCenter[6423:364053] [log]  [ERROR] RemoveNominatedCandidatePair:994 failed with invalid index input: -1, candidate pair count: 5
2018-01-23 17:54:01.412127+0700 testGameCenter[6423:364053] [log]  [ERROR] gckSessionCheckPendingConnections:1438 gckSessionCheckPendingConnections: iICEChecksLeft=0, iUnconnectedNodeCount=0, iDDsExpected=1
2018-01-23 17:54:01.420996+0700 testGameCenter[6423:363683] [log]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (714168510)
2018-01-23 17:54:01.427471+0700 testGameCenter[6423:363683] [log]  [ERROR] gckSessionCheckPendingConnections:1438 gckSessionCheckPendingConnections: iICEChecksLeft=0, iUnconnectedNodeCount=0, iDDsExpected=0
Match found
2018-01-23 17:54:05.012667+0700 testGameCenter[6423:363234] [MC] Lazy loading NSBundle MobileCoreServices.framework
2018-01-23 17:54:05.014147+0700 testGameCenter[6423:363234] [MC] Loaded MobileCoreServices.framework
Sending String: initRand,G:11851656342,0,0,0
DATA SENT!
2018-01-23 17:54:05.043478+0700 testGameCenter[6423:363234] [Error] Extension request cancelled with error: Error Domain=NSExtensionErrorDomain Code=-2 "Extension cancelled by host." UserInfo={NSLocalizedDescription=Extension cancelled by host.}
DATA SENT!

我希望你能帮助我弄清楚 code2 是什么以及如何阻止它。谢谢!

您从未设置匹配委托。