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!