Swift - Game Center not available

前端 未结 3 928
名媛妹妹
名媛妹妹 2021-01-14 17:45

I\'m trying to implement Game Center in my Swift game. I have a menu view controller, where the user can press a \"SCORES\" button, which should take them to the Game Center

3条回答
  •  甜味超标
    2021-01-14 18:04

    Assuming that you've enabled Game Center in your app and also added a leaderboard in iTunes Connect then you need to authenticate your player before you can show GC. Also, be sure that you've created a test user in iTunes Connect that you can use to log in to Game Center when the prompt appears.

    Your MenuViewController should authenticate the local player in viewDidLoad like so:

    class MenuViewController: UIViewController,
                GKGameCenterControllerDelegate
    {
        var leaderboardIdentifier: String? = nil
        var gameCenterEnabled: Bool = false
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
    
            //Your code that sets up your scene or other set up code
    
            //HERE IS WHERE YOU AUTHENTICATE
            authenticateLocalPlayer()
        }
    
        func authenticateLocalPlayer()
        {
            var localPlayer = getLocalPlayer() // see GKLocalPlayerHack.h
            localPlayer.authenticateHandler =
                { (viewController : UIViewController!, error : NSError!) -> Void in
                    if viewController != nil
                    {
                        self.presentViewController(viewController, animated:true, completion: nil)
                    }
                    else
                    {
                        if localPlayer.authenticated
                        {
                            self.gameCenterEnabled = true
                            localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler
                            { (leaderboardIdentifier, error) -> Void in
                                if error != nil
                                {
                                    print("error")
                                }
                                else
                                {
                                    self.leaderboardIdentifier = leaderboardIdentifier
                                    print("\(self.leaderboardIdentifier)") //in your example "VHS" should be returned
                                }
                            }
                        }
                        else
                        {
                            print("not able to authenticate fail")
                            self.gameCenterEnabled = false
    
                            if error
                            {
                                print("\(error.description)")
                            }
                            else
                            {
                                print(    "error is nil")
                            }
                        }
                    }
            }
        }
    
    
        func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
        {
            gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
        }    
    }
    

    After you've successfully authenticated then you should be able to present Game Center.

    Note the line: var localPlayer = getLocalPlayer() // see GKLocalPlayerHack.h

    To get that to work you need to do a little hack to get GKLocalPlayer to instantiate correctly in Swift.

    Create a new class in Objective-C and name the file GKLocalPlayerHack.h/m

    In the header put:

    //  GKLocalPlayerHack.h
    // Issue with GameKit and Swift
    // https://stackoverflow.com/questions/24045244/game-center-not-authenticating-using-swift
    
    #import 
    
    @interface GKLocalPlayerHack : NSObject
    
    GKLocalPlayer *getLocalPlayer(void);
    
    @end
    

    In the implementation file put:

    // GKLocalPlayerHack.m
    // Issue with GameKit and Swift
    // https://stackoverflow.com/questions/24045244/game-center-not-authenticating-using-swift
    
    #import "GKLocalPlayerHack.h"
    
    @implementation GKLocalPlayerHack
    
    GKLocalPlayer *getLocalPlayer(void)
    {
        return [GKLocalPlayer localPlayer];
    }
    
    @end
    

    Be sure to add:

    #import "GKLocalPlayerHack.h"
    

    To your bridging header. Credit to @marmph for his answer in this question: Game Center not authenticating using Swift

提交回复
热议问题