I am presenting a GKGameCenterViewController
in an SKScene that inherits from the following protocol.
protocol GameCenter {}
extension GameCenter where Self: SKScene {
func goToLeaderboard() {
let vc = GKGameCenterViewController()
vc.gameCenterDelegate = GameViewController()
vc.viewState = .leaderboards
vc.leaderboardIdentifier = "leaderboard"
view?.window?.rootViewController?.present(vc, animated: true, completion: nil)
}
}
While the GKGameCenterViewController
shows up perfect, when I try to dismiss by clicking the X in the top right corner, nothing happens. I assume this is because the reference to my original GameViewController
has been deallocated. How can I get this dismissal to work?
2
Answers
In order to present the
GKGameCenterViewController
on a SKScene, I needed to find the currently displayedUIViewController
reference and set this as the delegate. Here is the code I used and it works:According to Apple’s Documentation:
This means you need to implement the
gameCenterViewControllerDidFinish
method in your delegate and dismiss thegameCenterViewController
yourself.You should have something like this in your
GameViewController()