我正在使用 Game Center 的回合制比赛构建游戏。
我想显示所有可用匹配项的列表。我尝试使用loadMatchesWithCompletionHandler()
,但游戏数组返回为nil
,错误也返回为nil
。有一些正在进行的比赛。
这是我到目前为止所拥有的:
func authenticateLocalUser() {
if !gameCenterAvailable { return }
let player = GKLocalPlayer.localPlayer()
if player.authenticated == false {
player.authenticateHandler = {(viewController, error) -> Void in
if viewController != nil && self.presentingViewController != nil
{
self.presentingViewController!.presentViewController(viewController!, animated: true, completion: {
GKLocalPlayer.localPlayer().registerListener(self)
GKTurnBasedMatch.loadMatchesWithCompletionHandler({games, error in
print(games)
if games != nil {
print(games!.count)
}else {
print(error)
}
})
})
} else {
if player.authenticated == true {
GKLocalPlayer.localPlayer().registerListener(self)
GKTurnBasedMatch.loadMatchesWithCompletionHandler({games, error in
print(games)
if games != nil {
print(games!.count)
}else {
print(error)
}
})
}
}
}
} else {
print("already authenticated")
}
}
我什nil
至在创建新匹配时得到(不过它会打印我刚刚创建的匹配):
func findMatchWith(minPlayers: Int, maxPlayers: Int) {
if !gameCenterAvailable { return }
let request = GKMatchRequest()
request.minPlayers = minPlayers
request.maxPlayers = maxPlayers
request.defaultNumberOfPlayers = 2
GKLocalPlayer.localPlayer().loadFriendPlayersWithCompletionHandler({players, error in
if error != nil {return}
request.recipients?.append(players![0])
GKTurnBasedMatch.findMatchForRequest(request, withCompletionHandler: { match, error in
if error != nil {
print(error?.localizedDescription)
return
}
print(match)
GKTurnBasedMatch.loadMatchesWithCompletionHandler({games, error in
print(games)
if games != nil {
print(games!.count)
}else {
print(error?.localizedDescription)
}
})
})
})
}