iOS 14+
在 iOS 14 及以上我们应该使用:
/// Class method to submit a single score to multiple leaderboards
/// score - earned by the player
/// context - developer supplied metadata associated with the player's score
/// player - the player for whom this score is being submitted
/// leaderboardIDs - one or more leaderboard IDs defined in App Store Connect
@available(iOS 14.0, *)
open class func submitScore(_ score: Int, context: Int, player: GKPlayer, leaderboardIDs: [String], completionHandler: @escaping (Error?) -> Void)
这是一个例子:
GKLeaderboard.submitScore(
score,
context: 0,
player: GKLocalPlayer.local,
leaderboardIDs: ["leaderboardID"]
) { error in
print(error)
}
异步变体
上述 API 兼容 Swift 5.5 并发。如果您希望您的方法是异步,只需使用:
@available(iOS 14.0, *)
open class func submitScore(_ score: Int, context: Int, player: GKPlayer, leaderboardIDs: [String]) async throws
try await GKLeaderboard.submitScore(
score,
context: 0,
player: GKLocalPlayer.local,
leaderboardIDs: ["leaderboardID"]
)