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;
[[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!