在游戏中心匹配中获取错误:由主机取消

时间:2018-01-23 10:50:30

标签: ios swift xcode game-center

我正在开发一款非常简单的游戏来测试游戏中心的所有功能,并且更习惯Apple的GameKit以及所有这些功能。 这个非常简单的游戏给了我很多问题,而且我已经慢慢地,但肯定地经历了。我遇到了一个新问题,我希望有人可以帮助我 到目前为止,我可以做到以下几点:
- 连接到游戏中心&验证播放器
-Scoreboards&排行榜&提交分数
- 开始匹配过程(但不开始匹配) 无论如何,这是我的“开始比赛”的代码。这是在比赛请求结束后,找到了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)
    }

现在,在那里调用的ViewController的代码有以下启动代码:

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控制台中得到以下输出,我从未看到“receiveData”触发器

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是什么以及如何阻止它。谢谢!

1 个答案:

答案 0 :(得分:0)

您永远不会设置匹配委托。