Swift - GameCenter Integration for Leaderboards

I am making a game in Swift. I want you to be able to publish user ratings using GameCenter so that you can see the ratings of all my users. However, I spent the last day trying to figure out how to do this, but I did not find any helpful instructions.

I am new to iOS and Swift programming, and very little information on this subject is written in Objective-C.

Can someone help me integrate GameCenter into my application so that I can post user ratings to leaderboards so people can see?

EDIT: I already created a GameCenter leaderboard on iTunesConnect.

EDIT 2: I tried following this guide: http://www.appcoda.com/ios-game-kit-framework/ and convert it to Swift. I converted this:

-(void)authenticateLocalPlayer {
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
        if (viewController != nil) {
            [self presentViewController:viewController animated:YES completion:nil];
        }
        else{
            if ([GKLocalPlayer localPlayer].authenticated) {
                _gameCenterEnabled = YES;

                // Get the default leaderboard identifier.
                [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                    if (error != nil) {
                        NSLog(@"%@", [error localizedDescription]);
                    }
                    else{
                        _leaderboardIdentifier = leaderboardIdentifier;
                    }
                }];
            }

            else {
                _gameCenterEnabled = NO;
            }
        }
    };
}

in it:

func authenticateLocalPlayer() {
    var localPlayer : GKLocalPlayer!
    localPlayer.authenticateHandler = {(viewController : MenuViewController!, error : NSError!) -> Void in
        if viewController != nil {
            self.presentViewController(viewController, animated: true, completion: nil)
        } else {
            if localPlayer.authenticated {
                self.gameCenterEnabled = true

                localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifier : String!, error : NSError!) -> Void in
                    if error != nil {
                        println(error.localizedDescription)
                    } else {
                        self.leaderboardIdentifier = leaderboardIdentifier
                    }
                })

            } else {
                self.gameCenterEnabled = false
            }
        }
    }
}

but it crashes in this line:

localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in

Error message:

fatal error: nil unexpectedly found while deploying optional value

I can’t believe how difficult it is!

+4
source share
2 answers

Your specific problem has nothing to do with Game Center and is because you have a line var localPlayer : GKLocalPlayer!.

You declare an implicitly expanded option, and then immediately use it. This value is zero on the next line when trying to install localPlayer.authenticateHandler.

GKLocalPlayer :

var localPlayer = GKLocalPlayer()

, Game Center Swift. , localPlayer.authenticated true. :

http://www.openradar.me/17825348

: http://www.stuarticus.net/blog/2014/7/game-center-authentication-and-swift .

+5
+2

All Articles