'Can not dismiss GKGameCenterViewController when using SwiftUI UIViewControlllerRepresentable
I have a UIViewControllerRepresentable implementing a GKGameCenterViewController using SwiftUI. I have done all necessary setup with Game Center and this GKGameCenterViewController does appear when called in my SwiftUI view. However, I can't dismiss it. I know a Coordinator is required to implement the GKGameCenterControllerDelegate and I have done so. But it still does not dismiss. I'm not sure what I am doing wrong.
struct GameCenterView: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func updateUIViewController(_ uiViewController: GKGameCenterViewController, context: Context) {
}
func makeUIViewController(context: Context) -> GKGameCenterViewController {
let vc = GKGameCenterViewController()
vc.delegate = context.coordinator
return vc
}
class Coordinator: NSObject, GKGameCenterControllerDelegate, UINavigationControllerDelegate {
var parent: GameCenterView
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismiss(animated: true, completion: nil)
}
init(_ parent: GameCenterView) {
self.parent = parent
}
}
}
Solution 1:[1]
I have a fix.
delegate looks wrong.
You can do with gameCenterDelegate.
like this:
vc.gameCenterDelegate = context.coordinator
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shogo Ishida |
