0

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
        }
    }
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
iOS North
  • 1
  • 1

1 Answers1

0

I have a fix.

delegate looks wrong. You can do with gameCenterDelegate.

like this:

vc.gameCenterDelegate = context.coordinator