代码之家  ›  专栏  ›  技术社区  ›  Nathan Fallet

GKSavedGame未保存或正在加载

  •  1
  • Nathan Fallet  · 技术社区  · 4 年前

    我正在尝试使用保存玩家游戏数据 GKSavedGame 来自GameKit,但它不起作用,我没有收到任何错误。(数据是否未保存?数据是否未加载回?)

    问题之一是 GKSavedGame 没有很好的文档记录(在其他方面没有我们能找到的例子),所以我们真的不知道如何正确地实现它(例如,什么是最佳实践?)

    我正在使用一个简单的 struct GameData: Codable 存储游戏数据,我使用 JSONEncoder / JSONDecoder 。这是我的 GameService 用不起作用的部分上课:

    class GameService {
        
        // Shared instance
        
        static let shared = GameService()
        
        // Properties
        
        private(set) var isGameLoaded = false
        private(set) var gameData: GameData?
        
        // Methods
        
        func loadSavedGame(completionHandler: @escaping () -> Void) {
            // Check game is not loaded yet
            if isGameLoaded {
                completionHandler()
                return
            }
            
            // Get player
            let localPlayer = GKLocalPlayer.local
            if localPlayer.isAuthenticated {
                localPlayer.fetchSavedGames { games, error in
                    // Iterate saved games
                    var game: GKSavedGame?
                    for currentGame in games ?? [] {
                        if game == nil || game?.modificationDate ?? Date() < currentGame.modificationDate ?? Date() {
                            game = currentGame
                        }
                    }
                    
                    // If one found, load its data
                    if let game = game {
                        game.loadData { data, error in
                            if let data = data {
                                self.gameData = try? JSONDecoder().decode(GameData.self, from: data)
                            }
                            self.isGameLoaded = true
                            self.initGameData()
                            completionHandler()
                        }
                    } else {
                        self.isGameLoaded = true
                        self.initGameData()
                        completionHandler()
                    }
                }
            }
        }
        
        func saveGame() {
            // Get player
            let localPlayer = GKLocalPlayer.local
            if localPlayer.isAuthenticated, let gameData = gameData, let data = try? JSONEncoder().encode(gameData) {
                // Save its game data
                localPlayer.saveGameData(data, withName: "data") { savedGame, error in
                    if let error = error {
                        print(error.localizedDescription)
                    }
                }
            }
        }
        
        func initGameData() {
            // If game data is undefined, define it
            if gameData == nil {
                gameData = GameData()
            }
        }
        
        func gameEnded(level: Level, score: Int64) {
            // Here I edit my `gameData` object before saving the changes
            // I known that this method is called because the data is up to date in the UI
            // ...
            saveGame()
        }
        
    }
    

    此外,GameCenter在应用程序中启用了功能(以及排行榜等其他功能的锻炼)

    GameCenter Capability

    重新启动应用程序并调用 loadSavedGame 这个 gameData 属性未恢复到以前的状态。 这个代码出了什么问题?

    注意:我在模拟器和我自己的iPhone上测试了它(GameCenter和iCloud与其他应用程序协同工作的真实日常设备),它从来没有保存任何东西。

    1 回复  |  直到 4 年前
        1
  •  2
  •   Nathan Fallet    4 年前

    在功能、iCloud文档中启用iCloud并为应用程序创建容器后,它现在可以工作了。文档中没有任何关于此的内容。

    iCloud Capability