Basic GameCenter code not working or authenticating?

70 Views Asked by At

I have found multiple versions of the same code to authenticate a user in GameCenter, and every version seems to not want to work (when opening the app in the simulator, absolutely nothing pops up, there are no prompts from GameCenter, etc). This is mostly based off of what Apple has online, but this is my code (note that the viewfinder I am using is called AchievementsViewFinder, and I am just trying to ensure it either appears on that page or when the user first opens the app, either works):

import Foundation
import GameKit
import UIKit

class AchievementsViewController: UIViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        authenticateUser()
    }
    
    private func authenticateUser(){
        let player = GKLocalPlayer.local
        player.authenticateHandler = { AchievementsViewController, error in
            guard error == nil else{
                print(error?.localizedDescription ?? ""); return
            }
            self.presentViewController(vc!, animated: true, completion: nil)
        }
        
    }
    
    
    @IBAction func showAchievements(_ sender: Any) {
    }
    
    @IBAction func showLeaderboards(_ sender: Any) {
    }
    
    @IBAction func unlockAchievements(_ sender: Any) {
    }
    
    
    @IBAction func submit(_ sender: Any) {
    }
    
}

I think me adjusting the viewfinder is what is causing my code to not work, but after trying different solutions (mostly on stackoverflow) I realized that a lot of people have pretty different ways of tackling the same code. Any help would be appreciated, even if it's completely rewriting what I have.

1

There are 1 best solutions below

0
Kobe On

"I've tried summarizing it."

Issues

1. Authentication Handler Implementation

  • There may be issues with the implementation of the authentication handler, preventing the GameCenter prompt from appearing in the simulator.

Solutions

1. Check GameCenter Settings

  • Ensure GameCenter is properly set up as a feature in your app.

2. Verify Authentication UI Code

  • Confirm there are no issues with the code that displays the authentication UI.

3. Simulator Check

  • Make sure the simulator is logged in with a valid Apple ID that has GameCenter activated.

4. Review Authentication Handler Code

  • Examine and modify the implementation of the authentication handler, particularly how and when the view controller is displayed.
GKLocalPlayer.local.authenticateHandler = { viewController, error in
    if let viewController = viewController {
        // Present the view controller so the player can sign in.
        return
    }
    if error != nil {
        // Player could not be authenticated.
        // Disable Game Center in the game.
        return        
    }
    
    // Player was successfully authenticated.
    // Check if there are any player restrictions before starting the game.
            
    if GKLocalPlayer.local.isUnderage {
        // Hide explicit game content.
    }


    if GKLocalPlayer.local.isMultiplayerGamingRestricted {
        // Disable multiplayer game features.
    } 


    if GKLocalPlayer.local.isPersonalizedCommunicationRestricted {
        // Disable in game communication UI.
    }
    
    // Perform any other configurations as needed (for example, access point).
}

Reference