iOS platform game how to docking gamecenter

Source: Internet
Author: User

Now with the expanding market, active in the iOS platform game manufacturers in order to increase the stickiness of the players, as well as their own game for the future considerations have started to access the game platform Gamecenter Apple domestic company; Today I will put my butt gamecenter some of the experience and operation to share to everyone, the content is very simple also hope to have experience in this area can also share with me.

1.itunesConnect configuration:

Open Itunesconnect, check your app, find the Gamecenter option on your app page,


Click to open gamecenter Display effect:



Apple Gamecenter is divided into two pieces: "Leaderboards" and "achievements". First we'll add the leaderboard:



Add Achievements:



Each app has a maximum of 1000 points of achievement, the attribute hidden (hidden), and if it is set to Yes, it is invisible until the user has achieved or made some progress.

If you want to allow users to accept the challenge based on the achievements they have achieved, select the check box "can be obtained multiple times" when creating an achievement in itunes Connect.

Each achievement needs to be configured with a localized description; Each achievement has two descriptions, one shown before the user gets the achievement, and the other displayed after the user has achieved the achievement. You also need to provide a paid image for each achievement, size 512x512, (the achievements cannot be deleted after the app is published)


After editing the leaderboard and achievements, the results are finally in Itunesconnect:


For developers, Gamecenter must be tested to go live, and there is a sandbox hint when a program is logged in in a test environment.


Well, the basic configuration of the Itunesconnect section is finished, is not very simple. Let's take a look at the program section ~


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++

Apple provides gamekit.framework for everyone to access Gamecenter, import GameKit.h in classes that need to use Gamecenter, and add protocols to the. h file. Gkgamecentercontrollerdelegate ".


1. Game Center Manager

Creating a shared Game manager not only allows you to put Gamecenter functionality in a standalone class, but it also makes it easy to add gamecenter functionality to new projects.

Determine if Gamecenter is supported:

Support for gamecenter-(BOOL) isgamecenteravailable{    Class gcclass = (nsclassfromstring (@ "Gklocalplayer"));    NSString *reqsysver = @ "4.1";    NSString *currsysver = [[Uidevice currentdevice] systemversion];    BOOL osversionsupported = ([Currsysver compare:reqsysver options:nsnumericsearch]! = nsorderedascending);        Return (Gcclass && osversionsupported);}

2, authentication

Gamecenter is a service that requires authentication, and if you do not log in, you have to verify your identity before you can do nothing.

Authentication-(void) authenticatelocaluser{gklocalplayer *localplayer = [Gklocalplayer Localplayer];             Localplayer.authenticatehandler = ^ (Uiviewcontroller *viewcontroller, Nserror *error) {if (ViewController! = nil) {        [Self Presentviewcontroller:viewcontroller animated:yes completion:nil]; } else{if ([Gklocalplayer localplayer].authenticated) {//Get the default leaderboard I                                Dentifier. [[Gklocalplayer Localplayer] loaddefaultleaderboardidentifierwithcompletionhandler:^ (NSString *                        Leaderboardidentifier, Nserror *error) {if (Error! = nil) {                    NSLog (@ "%@", [Error localizeddescription]);            } else{}}];    } else{}}; }

3. User Change detection

User change detection-(void) registerfoeauthenticationnotification{    nsnotificationcenter *nc = [Nsnotificationcenter Defaultcenter];    [NC addobserver:self selector: @selector (authenticationchanged) Name: Gkplayerauthenticationdidchangenotificationname Object:nil];} -(void) authenticationchanged{    if ([Gklocalplayer localplayer].isauthenticated) {            }else{            }}

4. Submit a score

Once you have verified your identity with Gamecenter, you can submit your score.

To create a Gkstore object:

-(void) Reportscore: (int64_t) score forcategory: (nsstring*) category{gkscore    *scorereporter = [[Gkscore alloc] Ini Twithcategory:category];        Scorereporter.value = score;    [Scorereporter reportscorewithcompletionhandler:^ (Nserror *error) {        if (Error! = nil) {            NSData *savesocredata = [Nskeyedarchiver archiveddatawithrootobject:scorereporter];                        Failed to submit score, need to save and continue to submit            [self storescoreforlater:savesocredata];        } else{            NSLog (@ "submit success");        }    ];} -(void) Storescoreforlater: (NSData *) scoredata{    Nsmutablearray *savedscoresarray = [[Nsmutablearray alloc] Initwitharray:[[nsuserdefaults Standarduserdefaults] objectforkey:@ "Savedscores"];        [Savedscoresarray Addobject:scoredata];    [[Nsuserdefaults Standarduserdefaults] Setobject:savedscoresarray forkey:@ "Savedscores"];}

If the score submission is unsuccessful, you need to resubmit the score:

Resubmit score-(void) submitallsavedscores{    nsmutablearray *savedscorearray = [[Nsmutablearray alloc] initwitharray:[[ Nsuserdefaults Standarduserdefaults] objectforkey:@ "Savedscores"];        [[Nsuserdefaults Standarduserdefaults] removeobjectforkey:@ "Savedscores"];        For (NSData *scoredata in Savedscorearray) {        Gkscore *scorereporter = [Nskeyedunarchiver unarchiveobjectwithdata: Scoredata];                [Scorereporter reportscorewithcompletionhandler:^ (Nserror *error) {            if (Error! = nil) {                 NSData *savesocredata = [Nskeyedarchiver archiveddatawithrootobject:scorereporter];                Failed to submit score, need to save and continue to submit                [self storescoreforlater:savesocredata];            } else{                NSLog (@ "submit success");}}        ];}    }

5. Show leaderboard

Create Gklocalboardviewcontroller to display the leaderboard.

-(void) showgamecenter{    gkgamecenterviewcontroller *gameview = [[Gkgamecenterviewcontroller alloc] init];    if (Gameview! = nil) {        gameview.gamecenterdelegate = self;                [Gameview setleaderboardcategory:@ "Com.xxxx.test"];        [Gameview Setleaderboardtimescope:gkleaderboardtimescopealltime];                [Self Presentviewcontroller:gameview animated:yes completion:^{                    }];    } -(void) Gamecenterviewcontrollerdidfinish: (Gkgamecenterviewcontroller *) gamecenterviewcontroller{    [self Dismissviewcontrolleranimated:yes Completion:nil];}

6. Scoring achievements

The challenge allows users to challenge their scores or accomplishments through Gamecenter to the player. The challenges are divided into four categories: invalid, pending, closed, declined.

-(void) Reportachievment: (NSString *) identifier Withpercentagecomplete: (double) percentcomplete{    gkachievement *achievement = [[Gkachievement alloc] initwithidentifier:identifier];        [Achievement Setpercentcomplete:percentcomplete];        [Achievement reportachievementwithcompletionhandler:^ (Nserror *error) {        if (Error! = nil) {            NSLog (@ "error:%@", [Error localizeddescription]);        } else{            NSLog (@ "Submit achievement success");        }    ];}


Sample Demo:

1. Log in in the sandbox environment Gamecenter


When the login is successful, a banner "Welcome ..." will appear above, and clicking on the Gamecenter app will show you and the information about your game.





3. Leaderboard Interface



4. Achievements

The icon for the achievement is the icon configured in the background;


5 . Add Friends

Click on the "Plus" button in the upper right corner, will pop up an interface to add friends, enter the other appleid can send friend request.


6. Launching the challenge


The other party receives the challenge message push interface


Well, gamecenter access to this side of the end, if in the development of new discoveries I will be in time with the new article, welcome everyone comments share their views.



















iOS platform game how to docking gamecenter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.