iOS - GameKit

Gamekit is a structure that provides innovator panel, success, and more functions to an iOS program. In this guide, we will be describing the actions engaged in including a innovator panel and upgrading the ranking.

Steps Involved

Step 1. In iTunes link, make sure that you have a exclusive App ID and when we make the program upgrade with the package ID and rule deciding upon in Xcode with corresponding provisioning information.

Step 2. Build a new program and upgrade program information. You can know more about this in apple-add new applications certification.

Step 3. Installation a innovator panel in Handle Game Middle of your application's page where add only one leaderboard and provides leaderboard ID and ranking Type. Here we give innovator panel ID as tutorialsPoint.

Step 4. The next actions are relevant to managing rule and developing UI for our program.

Step 5. Build only one perspective program and get into the package identifier is the identifier specified in iTunes link.

Step 6. Update the ViewController.xib as proven below −


Step 7. Choose your venture computer file, then select objectives and then add GameKit.framework.

Step 8. Create IBActions for the control buttons we have included.

Step 9. Upgrade the ViewController.h computer file as follows −

#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface ViewController : UIViewController
<GKLeaderboardViewControllerDelegate>

-(IBAction)updateScore:(id)sender;
-(IBAction)showLeaderBoard:(id)sender;

@end
Step 10. Update ViewController.m as follows −
#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

When we run the program, we'll get the following outcome −


When we just click "show innovator board", we would get a display just like the following −


When we just click "update score", the ranking will be modified to our innovator panel and we will get an aware as proven below −


1 comments :

Write comments
veera
AUTHOR
1 September 2020 at 23:00 delete

Thank you for sharing this awesome blog with us.
Really this blog helps me alot.

keep sharing more posts with us.

Thank you...

ios app development course

Reply
avatar