How to sign in a guest player in GameKit

120 Views Asked by At

I want users to still be able to play online when they don't feel like logging in using Game Center. This is how I thought it would work using an anonymous / guest player:

GKLocalPlayer.local.authenticateHandler = { viewController, error in
    if let viewController {
        self.present(viewController, animated: true)
    } else {
        self.dismiss(animated: true)
        
        // Create a guest player if there was an error logging in. The anonymous / guest 
        // user is created when the user taps "cancel" on the login view controller.
        // (This isn't how it is in my game there are buttons and stuff. This is just more 
        //  straightforward for stackoverflow)
        if error != nil {
            GKLocalPlayer.anonymousGuestPlayer(withIdentifier: UUID().uuidString)
        }
                
        let request = GKMatchRequest()
        request.minPlayers = 2
        request.maxPlayers = 4
        let matchmakerVC = GKMatchmakerViewController(matchRequest: request)!
        matchmakerVC.matchmakerDelegate = self
                
        GKLocalPlayer.local.register(self)
        self.present(matchmakerVC, animated: true)
    }
}

However, when the guest player is created using GKLocalPlayer.anonymousGuestPlayer(withIdentifier: UUID().uuidString), I get an alert on my phone saying "Multiplayer Unavailable. Player is not signed in." How can I sign in the user as a guest player and allow for online matchmaking?

0

There are 0 best solutions below