I'm trying to save game data across devices. I'm trying to understand if GameCenter has the ability to do this.. I was reading from the docs..
To provide saved game data between devices, developers need to create their own solutions, either using their own servers or using a secondary service, such as Facebook. Using the new saved game methods found in GKSavedGame, GKLocalPlayer, and GKSavedGameListener classes, developers can easily save game data to iCloud.
However, when I fetch saved games on separate devices, I get different results. Both devices are using the same GameCenter and iCloud accounts. Note: When I delete the app locally on a device, I am able to retrieve the local GameCenter data.
var localPlayer: GKLocalPlayer {
return GKLocalPlayer.local
}
func authenticateLocalPlayer(completion: @escaping((_ gameCenterLoginVC: UIViewController?,_ gameCenterEnabled: Bool ) -> Void)) {
localPlayer.authenticateHandler = { [weak self] (viewController, error) -> Void in
guard let self = self else { return }
if viewController != nil {
completion(viewController, true)
} else if self.localPlayer.isAuthenticated {
self.gcEnabled = true
self.fetchSavedGames(completion: { (success) in
completion(nil, true)
})
} else {
completion(nil, false)
}
}
}
private func fetchSavedGames(completion: @escaping((_ success: Bool) -> Void)) {
localPlayer.fetchSavedGames { (savedGames, error) in
// savedGames here has different results across devices.
guard let savedGame = savedGames?.sorted(by: { (game1, game2) -> Bool in
game1.modificationDate ?? Date.init(timeIntervalSince1970: 0) > game2.modificationDate ?? Date.init(timeIntervalSince1970: 0)
}).first else {
completion(false)
return
}
savedGame.loadData(completionHandler: { (data, error) in
guard let data = data, error == nil else {
completion(false)
return
}
User.shared.loadUserWith(data: data)
completion(true)
})
}
}