I only started developing with Swift last week but I already made a simple game and published it on the App Store. However, I would love to add a global leaderboard for the highest scores that players get.
I already have a local high score ready to be used, I just need to submit it to the leaderboard (which I set up on App Store Connect) and display the leaderboard.
I only use SwiftUI in my project as it seems to be the new standard going forward, therefore I am not very familiar with how Storyboard functions.
All the examples for displaying the leaderboard are done with Storyboard and I have tried for hours to make a version that works for my project but couldn't figure it out.
Can anyone help me by showing me an example of how to show the leaderboard using SwiftUI Views only?
Here is my code, I am able to authenticate the player on Game Center but don't know how to make a function (or View) that displays the leaderboard.
I use viewState to determine which View to display in another file. Each View is stored in a separate file. I can make a LeaderboardView file if it's needed for this purpose.
import SwiftUI
import GameKit
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
struct ScoreView: View {
@Binding var viewState:String
@Binding var highestScoreString:String
var body: some View {
VStack{
Spacer()
Text("HIGHEST SCORE:")
Text(highestScoreString)
Spacer()
Button(action: {
let highestScore = Int(highestScoreString)
authenticatePlayer()
// submit highest score here
// show leaderboard here
}){
Text("LEADERBOARD")
}
Button(action: {
viewState = "main"
}){
Text("MAIN MENU")
}
Spacer()
}
}
}
func authenticatePlayer() {
localPlayer.authenticateHandler = {(vc, error) -> Void in
guard error == nil else{
print(error?.localizedDescription ?? "")
return
}
GKAccessPoint.shared.isActive = localPlayer.isAuthenticated
}
}