Easy Tutorial
❮ Ios File Ios In App Purchase ❯

IOS GameKit


Introduction

GameKit is a commonly used framework in the iOS SDK. Its core functionalities include:

Example Steps

  1. When linking with iTunes, ensure you have a unique App ID (unique App ID). The App ID is required when updating the bundle ID in our application and during Xcode code signing and the corresponding provisioning profile.

  2. Create a new application and update the application information. More information can be found in the new application documentation.

  3. Open your application, click on the Manage Game Center option. Once inside, click Enable Game Center to activate your Game Center. Next, set up your Leaderboard and Achievements.

  4. The next step involves handling code and creating the user interface for our application.

  5. Create a single view application and enter the bundle identifier.

  6. Update ViewController.xib as shown.

  7. Select the project file, then select the target, and add GameKit.framework.

  8. Create IBActions for the added buttons.

  9. Update ViewController.h file as shown:

    #import <UIKit/UIKit.h>
    #import <GameKit/GameKit.h>
    
    @interface ViewController : UIViewController
    <GKLeaderboardViewControllerDelegate>
    
    -(IBAction)updateScore:(id)sender;
    -(IBAction)showLeaderBoard:(id)sender;
    
    @end
    
  10. Update ViewController.m as shown:

    #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 Successfully" 
            delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
    
        }];
    }
    -(IBAction)updateScore:(id)sender{
        [self updateScore:200 forLeaderboardID:@"tutorialpro"];
    }
    -(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
❮ Ios File Ios In App Purchase ❯