Reputation: 584
I'm trying to save match data with the Game Centre default function. The following function works pretty fine and things got saved.
self.myMatch?.saveCurrentTurn(withMatch: dataToSend, completionHandler: { (e) in
print(e ?? "No Error:")
})
This method was introduced in IOS 6, and at that time they were sending push notifications to the opponent as like the endTurnWithNextParticipant
, but now in the current IOS 10, if they have removed feature of sending push notifications, but there should any other way to detect matchData
update on opponents side.
Upvotes: 1
Views: 144
Reputation: 1949
Yes, the newer way is to use the player listener to detect this kind of change in the match. I have created an example project of a turn based GameKit game that you are welcome to use as a skeleton. The key function is found in the ViewController.swift
file:
/// Activates the player's turn.
open func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool) {
print("***** player received turn event \(didBecomeActive ? "and became active for" : "for") match \(match.shortID)")
dismiss(animated: true, completion: nil)
if didBecomeActive {
// This event activated the application. This means that the user
// tapped on the notification banner and wants to see or play this
// match now.
print("***** attaching the model to this match")
model?.match = match
} else if model?.match?.matchID == match.matchID {
// This is the match the user is currently playing,
// laoding the game model below iwll update to show the latest state
print("***** refreshing data for this match")
} else if match.currentParticipant?.player?.playerID == model?.localPlayerID {
// It became the player's turn in a different match,
// prompt the player to switch to the new match
print("***** ignoring player's turn in match \(match.shortID)")
gameTextView.text.append("\n\nFYI, it is your turn in another match.")
return
} else {
// Something has changed in another match,
// but not sure what.
print("***** ignoring new data for match \(match.shortID)")
gameTextView.text.append("\n\nFYI, something has changed in another match.")
return
}
print("***** loading match")
GameModel.loadGameModel(match: match) { model, error in
guard self.isNotError(error, during: "model loading") else { return }
guard let model = model else {
print("***** no model created")
self.gameLabel.text = "Error setting up game"
self.thereHasBeenAnError = true
return
}
print("***** match load succeeded")
self.model = model
self.model?.checkForWin() { won, error in
guard self.isNotError(error, during: "end match request") else { return }
if won {
self.gameLabel.text = "You won!"
self.turnButton.isEnabled = false
self.resignButton.isEnabled = false
}
}
}
}
The references to the game model
will make much more sense in the context of the whole example project, but to your question: notice the "refreshing data for this match" line? That is how you detect new incoming match data. You can do with it whatever you need.
Upvotes: 3