gamekit is a framework that provides leader board, achievements, and more features to an ios application. in this tutorial, we will be explaining the steps involved in adding a leader board and updating the score.
steps involved
step 1 − in itunes connect, ensure that you have a unique app id and when we create the application update with the bundle id and code signing in xcode with corresponding provisioning profile.
step 2 − create a new application and update application information. you can know more about this in apple-add new apps documentation.
step 3 − setup a leader board in manage game center of your application's page where add a single leaderboard and give leaderboard id and score type. here we give leader board id as tutorialspoint.
step 4 − the next steps are related to handling code and creating ui for our application.
step 5 − create a single view application and enter the bundle identifier is the identifier specified in itunes connect.
step 6 − update the viewcontroller.xib as shown below −
step 7 − select your project file, then select targets and then add gamekit.framework.
step 8 − create ibactions for the buttons we have added.
step 9 − update the viewcontroller.h file as follows −
#import <uikit/uikit.h> #import <gamekit/gamekit.h> @interface viewcontroller : uiviewcontroller <gkleaderboardviewcontrollerdelegate> -(ibaction)updatescore:(id)sender; -(ibaction)showleaderboard:(id)sender; @end
step 10 − update viewcontroller.m as follows −
#import "viewcontroller.h"
@interface viewcontroller ()
@end
@implementation viewcontroller
- (void)viewdidload {
[super viewdidload];
if([gklocalplayer localplayer].authenticated == no) {
[[gklocalplayer localplayer]
authenticatewithcompletionhandler:^(nserror *error) {
nslog(@"error%@",error);
}];
}
}
- (void)didreceivememorywarning {
[super didreceivememorywarning];
// dispose of any resources that can be recreated.
}
- (void) updatescore: (int64_t) score
forleaderboardid: (nsstring*) category {
gkscore *scoreobj = [[gkscore alloc]
initwithcategory:category];
scoreobj.value = score;
scoreobj.context = 0;
[scoreobj reportscorewithcompletionhandler:^(nserror *error) {
// completion code can be added here
uialertview *alert = [[uialertview alloc]
initwithtitle:nil message:@"score updated succesfully"
delegate:self cancelbuttontitle:@"ok" otherbuttontitles: nil];
[alert show];
}];
}
-(ibaction)updatescore:(id)sender {
[self updatescore:200 forleaderboardid:@"tutorialspoint"];
}
-(ibaction)showleaderboard:(id)sender {
gkleaderboardviewcontroller *leaderboardviewcontroller =
[[gkleaderboardviewcontroller alloc] init];
leaderboardviewcontroller.leaderboarddelegate = self;
[self presentmodalviewcontroller:
leaderboardviewcontroller animated:yes];
}
#pragma mark - gamekit delegates
- (void)leaderboardviewcontrollerdidfinish:
(gkleaderboardviewcontroller *)viewcontroller {
[self dismissmodalviewcontrolleranimated:yes];
}
@end
output
when we run the application, we'll get the following output −
when we click "show leader board", we would get a screen similar to the following −
when we click "update score", the score will be updated to our leader board and we will get an alert as shown below −