Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Game Center authentication doesn't prompt user to login

I have the below code to authenticate a local player in Game Center in my SwiftUI app. I want Game Center to prompt user to login if the player is not already login in but this doesn't happen.

class AppSettings: UINavigationController {
    func authenticateUser() {
        let localPlayer = GKLocalPlayer.local
        localPlayer.authenticateHandler = { vc, error in
            guard error == nil else {
                print(error?.localizedDescription ?? "")
                return
            }
        }
    }
}

What could be the problem? I also read about using UIViewControllerRepresentable somewhere in my class to integrate UIKit's ViewController into SwiftUI but I don't understand how I can use it. Can someone help me out?

like image 578
Heyman Avatar asked Sep 25 '20 17:09

Heyman


1 Answers

I didn't get anyone to answer my question correctly and after days of digging I found a solution. So I had to use the UIKit implementation like below and create a wrapper around it using UIViewControllerRepresentable in the GameCenterManager Struct. After that all I had to do was call GameCenterManager() inside my SwiftUI view in a ZStack and the job is done!

import SwiftUI
import UIKit
import GameKit


class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        authenticateUser()
    }
    let localPlayer = GKLocalPlayer.local
    func authenticateUser() {
        
        localPlayer.authenticateHandler = { vc, error in
            guard error == nil else {
                print(error?.localizedDescription ?? "")
                return
            }
            if vc != nil {
                self.present(vc!, animated: true, completion: nil)
            }
            if #available(iOS 14.0, *) {
                GKAccessPoint.shared.location = .bottomLeading
                GKAccessPoint.shared.showHighlights = true
                GKAccessPoint.shared.isActive = self.localPlayer.isAuthenticated
                
                // Fallback on earlier versions
            }
        }
    }
}


struct GameCenterManager: UIViewControllerRepresentable {
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<GameCenterManager>) -> ViewController {
        
        
        let viewController = ViewController()
        return viewController
        
    }
    
    func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext<GameCenterManager>) {
        
    }
    
}
like image 139
Heyman Avatar answered Nov 15 '22 07:11

Heyman