I’m converting the Beginning Turn-Based Gaming with iOS 5 tutorial to swift and iOS 9. I got it working, but I would like to add a list of all the matches the current player is playing (either started or was invited to).
I found GKTurnBasedMatch.loadMatchesWithCompletionHandler
, but I can’t get it to work. I just get nil
for the list of games, and no errors.
I’ve tried putting the code in different places, including right after creating a match, just to test if it will list the game I just created:
func findMatchWith(minPlayers: Int, maxPlayers: Int, viewController: UIViewController) {
if !gameCenterAvailable { return }
let request = GKMatchRequest()
request.minPlayers = minPlayers
request.maxPlayers = maxPlayers
request.defaultNumberOfPlayers = 2
// Instead of using the default Game Center View Controller,
// I'm creating a game programmatically, using their first friend (matches are for just 2 players)
GKLocalPlayer.localPlayer().loadFriendPlayersWithCompletionHandler({players, error in
if error != nil {
print(error?.localizedDescription)
return
}
request.recipients = [players![0]]
GKTurnBasedMatch.findMatchForRequest(request, withCompletionHandler: { match, error in
if error != nil {
print(error?.localizedDescription)
return
}
// it does print the match I just created
print(match)
self.currentMatch = match
GKTurnBasedMatch.loadMatchesWithCompletionHandler({games, error in
// this prints nil
print(games)
if games != nil {
print(games!.count)
}else {
// This also prints nil
print(error?.localizedDescription)
}
})
if match != nil {
let firstParticipant = match!.participants![0]
if firstParticipant.lastTurnDate == nil {
print("new Match")
self.delegate?.enterNewGame(match!)
} else {
if match!.currentParticipant!.player == GKLocalPlayer.localPlayer(){
print("existing Match")
self.delegate?.takeTurn(match!)
} else {
print("not my turn")
self.delegate?.layoutMode(match!)
}
}
}
})
})
}
Could it be something to do with the setting for the game in iTunes Connect? or the sandbox accounts? Is there a property or listener that I have to set for this to work? I’m stumped.