Swift - Integrate GameCenter to use leaderboards
Asked Answered
K

2

5

I am making a game in Swift. I want to be able to post the users' score using GameCenter, so that scores from all my users' can be seen. However, I have spent the past day trying to figure out how to do this, but I haven't found any helpful instructions.

I am pretty new to iOS programming, and Swift, and of the very little amount of information on this subject, it's all written in Objective-C.

Can anyone help me integrate GameCenter into my app, so that I can post users scores to the leaderboards for people to see?

EDIT: I have already created a GameCenter leaderboard on iTunesConnect.

EDIT 2: I have tried following this tutorial: http://www.appcoda.com/ios-game-kit-framework/ and converting it to Swift. I have 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;
            }
        }
    };
}

into this:

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 on this line:

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

The Error message is:

fatal error: unexpectedly found nil while unwrapping an Optional value

I can't believe how hard this is!

Kanara answered 13/8, 2014 at 16:43 Comment(4)
"and of the very little amount of information on this subject, it's all written in Objective-C." - so what? the API is all the same, difference is only in syntax.Costrel
If you are new to it, i recommend buying a few books and learning how to use the code properly. I learnt how to use leaderboards in the games book from Ray Wenderlich. It is in objective-c, but as said above, it is incredibly easy to convert over as it is merely a syntax changeStarstarboard
Okay, I understand that. I've just tried for past hour trying to convert an Objective-C tutorial to Swift, and it's incredibly difficult, especially for a beginner like me! See my edit.Kanara
Hi I just read your post about game center. I'm trying to integer it in my game with swift too. I'm not a really good programer and it's impossible for me to translate Objective-C to swift as you did. I read the very good tutorial you gave but as you said it's in objective C. So do you know where I can found all the code and explanation in swift? Thank you very much !!Muskogean
S
5

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

You're declaring an implicitly unwrapped optional and then using it right away. It's value is nil in the subsequent line when you try to set localPlayer.authenticateHandler.

Instead you should instantiate GKLocalPlayer like so:

var localPlayer = GKLocalPlayer()

Note that there are currently issues with Game Center and Swift. Your code is going to work but localPlayer.authenticated never gets set to true. This issue is tracked here:

http://www.openradar.me/17825348

Credit to: http://www.stuarticus.net/blog/2014/7/game-center-authentication-and-swift for filing the radar ticket.

Snakemouth answered 16/8, 2014 at 0:15 Comment(1)
This issue seems to be resolved as of Xcode 6 beta 6Shurlock
P
2

You can use that, I create a simple class for iOS game center in github Easy Class Game Center Swift https://github.com/DaRkD0G/Easy-Game-Center-Swift

Poverty answered 28/12, 2014 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.