IOS GameKit
IOS GameKit
Introduction
GameKit is a commonly used framework in the iOS SDK. It has three core functions:
- Interactive game platform Game Center,
- P2P device communication function
- In-Game Voice.
Instance steps
1. Please make sure to have a unique App ID (unique App ID) when linking to iTunes. The App ID is used when our application updates the bundle ID and in Xcode Code signing and corresponding configuration files are required.
2. Create new applications and update application information. Learn more about this in the Add a new application documentation.
3. Open the application you applied for and click the Manage Game Center option. After entering, click Enable Game Center to make your Game Center effective. Next set up your own Leaderboard and Achievements.
4. The next step involves working on the code and creating the user interface for our application.
5. Create a single view application and enter bundle identifier.
6. Update ViewController.xib as shown below
7. Select the project file, then select the target, and add GameKit.framework
8.Create IBActions for the added buttons
9.Update the ViewController.h file as shown below
#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 below
#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
Run the application, the output results are as follows
When we click to display the leaderboard, the screen displays as follows:
When we click Update Score, the score will be updated to our rankings, and we will get a message, as shown below