iOS GameKit

Gamekit is a framework that provides leader board, achievements, and more features to an iOS application. In this chapter, we will be discussing the steps involved in adding a leader board and updating the score.

Steps Involved

  • Step 1. In iTunes connect, ensure that we 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. We need to create a new application and update application information. We can know more about this in apple-add new apps documentation.
  • Step 3. We need toSetup a leader board in Manage Game Center of our application’s page where add a single leaderboard and give leaderboard ID and score Type. Here we give leader board ID as WisdomJobs.
  • Step 4. The next steps are related to handling code and creating UI for our application.
  • Step 5. We need toCreate a single view application and enter the bundle identifier is the identifier specified in iTunes connect.
  • Step 6.We need to update the ViewController.xib as displayed below −

iOS - GameKit

  • Step 7. Select our 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 below−
1
#import <UIKit/UIKit.h>
2
#import <GameKit/GameKit.h>
3
@interface ViewController : UIViewController
4
<GKLeaderboardViewControllerDelegate>
5
-(IBAction)updateScore:(id)sender;
6
-(IBAction)showLeaderBoard:(id)sender;
7
@end
8
  • Step 10. Update ViewController.m as follows −

1
#import "ViewController.h"
2
@interface ViewController ()
3
@end
4
@implementation ViewController
5
- (void)viewDidLoad
6
{
7
  [super viewDidLoad];
8
  if([GKLocalPlayer localPlayer].authenticated == NO)
9
  {
10
   [[GKLocalPlayer localPlayer]
11
   authenticateWithCompletionHandler:^(NSError *error)
12
   {
13
    NSLog(@"Error%@",error);
14
   }];
15
  }
16
}
17
- (void)didReceiveMemoryWarning
18
{
19
  [super didReceiveMemoryWarning];
20
  // Dispose of any resources that can be recreated.
21
}
22
- (void) updateScore: (int64_t) score
23
forLeaderboardID: (NSString*) category
24
{
25
  GKScore *scoreObj = [[GKScore alloc]
26
  initWithCategory:category];
27
  scoreObj.value = score;
28
  scoreObj.context = 0;
29
  [scoreObj reportScoreWithCompletionHandler:^(NSError *error) {
30
    // Completion code can be added here
31
    UIAlertView *alert = [[UIAlertView alloc]
32
    initWithTitle:nil message:@"Score Updated Succesfully"
33
    delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
34
    [alert show];
35
  }];
36
}
37
-(IBAction)updateScore:(id)sender{
38
  [self updateScore:200 forLeaderboardID:@"wisdomjobs"];
39
}
40
-(IBAction)showLeaderBoard:(id)sender{
41
  GKLeaderboardViewController *leaderboardViewController =
42
  [[GKLeaderboardViewController alloc] init];
43
  leaderboardViewController.leaderboardDelegate = self;
44
  [self presentModalViewController:
45
  leaderboardViewController animated:YES];
46
}
47
#pragma mark - Gamekit delegates
48
- (void)leaderboardViewControllerDidFinish:
49
(GKLeaderboardViewController *)viewController{
50
  [self dismissModalViewControllerAnimated:YES];
51
}
52
@end
53

Output

On running the application, we’ll get the below output-

iOS GameKit

On clicking “show leader board”, we will be getting a screen, which is similar to the below screen −

iOS GameKit

On clicking “update score”, the score will get updated to our leader board and we will get an alert as displayed below −

iOS GameKit