IOS GameKit
Introduction
GameKit is a commonly used framework in the iOS SDK. Its core functionalities include:
- The interactive gaming platform Game Center,
- P2P device communication functionality,
- In-Game Voice.
Example Steps
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.
Create a new application and update the application information. More information can be found in the new application documentation.
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.
The next step involves handling code and creating the user interface for our application.
Create a single view application and enter the bundle identifier.
Update ViewController.xib as shown.
Select the project file, then select the target, and add GameKit.framework.
Create IBActions for the added buttons.
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
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