May 21, 2021 iOS Development Manual
GameKit is a commonly used framework in the iOS SDK. Its core functions are three:
1. Make sure you have a unique App ID (unique App ID) when linking to iTunes, which you need to use when our app updates the bundle ID and when you sign the Xcode code with the appropriate profile.
2. Create new applications and update application information. You can learn more when you add a new application document.
3. Open the app you applied for and click on the Manage Game Center option. C lick on The Enable Game Center to make your Game Center effective. Next set up your own Leaderboard and Achievements.
4. The next step involves working with the code and creating a user interface for our application.
5. Create a style view application and enter bundle identifier.
6. Update ViewController.xib, as shown below
7. Select the project file, then select the target, and then add GameKit.framework
8. Create IBEActions 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 the 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
Run the application and the output is as follows
When we click to display the leaderboard, the screen looks like this:
When we click update the score, the score will be updated to our leaderboard and we will get a message as shown in the following image