ios - 在 Game Center 比赛中出现错误 : cancelled by host

标签 ios swift xcode game-center

我正在开发一个非常简单的游戏来测试 Game Center 的所有功能,并更加习惯 Apple 的 GameKit 和所有这些东西。 这个极其简单的游戏给我带来了很多问题,我一直在缓慢但坚定地解决。我遇到了一个新问题,希望这里有人可以帮助我。
到目前为止我可以执行以下操作:
-连接到游戏中心并验证玩家
-记分板和排行榜以及提交分数
-开始匹配过程(但不开始比赛)。
无论如何,这是我的“开始比赛”代码。这是在比赛请求完成并找到 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 是什么以及如何阻止它。谢谢!

最佳答案

您从未设置过匹配委托(delegate)。

关于ios - 在 Game Center 比赛中出现错误 : cancelled by host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48399851/

相关文章:

ios - UICollectionView 添加按钮到单元格

ios - 为什么 UIButton 需要点击两次来改变它的图像

iphone - 线程 1 : SIGBART, 再次

iphone - UIView的setNeedsLayout、layoutIfNeeded和layoutSubviews之间有什么关系?

c++ - xcode 10 和 11,没有高级语法着色 (c++)

ios - 解决两个 setter 相互影响的方法

ios - RxSwift 简单 withLatestFrom 不触发

arrays - 在 Swift 中, append 数组的效率如何?

php - 使用 Swift 2.0 将图像上传到 PHP

ios - 如何从一个文件控制所有导航栏?