How IOS games interact with GameCenter
As the mobile game market continues to expand, game manufacturers active on the IOS platform want to increase players' stickiness, as well as for the future of their games, they have begun to access Apple's domestic gaming platform GameCenter. Today, I will share some of my experiences and operations with GameCenter, the content is very simple and I hope you can share it with me if you have experience in this area.
1. itunesConnect Configuration:
Open ItunesConnect, select your application, and find the GameCenter option on the application page,
Click Open GameCenter to display the effect:
Apple GameCenter is divided into two parts: "rankings" and "achievements ". First, let's add a ranking list:
Add achievements:
The number of achievement points of each application is up to 1000 points, and the property is Hidden. If you set it to YES, the result is invisible before the user gets the achievement or makes some progress.
To enable users to accept the challenges based on their achievements, you need to select the check box "can be obtained multiple times" when creating iTunes Connect ".
Each achievement requires a localized description. Each achievement has two descriptions. One is displayed before the user gets the achievement, and the other is displayed after the user gets the achievement. In addition, you need to provide a paid image of 512X512 for each achievement (the achievement cannot be deleted after the application is released)
After the rankings and achievements are edited separately, the final effect in ItunesConnect is as follows:
For developers, GameCenter must be tested before it can be launched. A sandBox prompt appears when a program that has not been launched is logged on to the test environment.
Now, the basic configuration of itunesConnect is complete. Is it very easy. Next, let's talk about the program part ~
// ++ ++ ++ ++
Apple provides GameKit. framework for you to access GameCenter. You must import GameKit. h to all the classes that require GameCenter. In the. h file, add the Protocol "GKGameCenterControllerDelegate ".
1. Game Center Manager
Creating a shared game manager not only puts the GameCenter function in a separate class, but also easily adds the GameCenter function to a new project.
Determine whether GameCenter is supported:
// Whether 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 identity verification. If you do not log on to GameCenter, you must first verify your identity. Otherwise, nothing can be done.
// 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 identifier. [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler: ^ (NSString * leaderboardIdentifier, NSError * error) {if (error! = Nil) {NSLog (@ "% @", [error localizedDescription]) ;}else {}}] ;}else {}}};}
3. User Change Detection
// User change detection-(void) registerFoeAuthenticationNotification {nsicationicationcenter * nc = [nsnotificationicationcenter defacenter center]; [nc addObserver: self selector: @ selector (authenticationChanged) name: Your object: nil];}-(void) authenticationChanged {if ([GKLocalPlayer localPlayer]. isAuthenticated) {} else {}}
4. Submit score
After you verify your identity with GameCenter, you can submit the score.
Create a GKStore object:
-(Void) reportScore: (int64_t) score forCategory: (NSString *) category {GKScore * scoreReporter = [[GKScore alloc] initWithCategory: category]; scoreReporter. value = score; [scoreReporter reportScoreWithCompletionHandler: ^ (NSError * error) {if (error! = Nil) {NSData * saveSocreData = [NSKeyedArchiver archivedDataWithRootObject: scoreReporter]; // If the score cannot be submitted, save the score and submit [self storeScoreForLater: saveSocreData]; else {NSLog (@ "submitted successfully") ;}}] ;}- (void) storeScoreForLater :( NSData *) scoreData {NSMutableArray * savedScoresArray = [[NSMutableArray alloc] initWithArray: [[NSUserDefaults standardUserDefaults] objectForKey: @ "savedScores"]; [incluaddobject: scoreData]; [[NSUserDefaults standardUserDefaults] setObject: incluforkey: @ "savedScores"];}
If the score cannot be submitted, You need to submit the score again:
// Resubmit the score-(void) into {NSMutableArray * savedScoreArray = [[NSMutableArray alloc] initWithArray: [[NSUserDefaults standardUserDefaults] objectForKey: @ "savedScores"]; [[NSUserDefaults standardUserDefaults] removeObjectForKey: @ "savedScores"]; for (NSData * scoreData in savedScoreArray) {GKScore * scoreReporter = [revoke privileges: scoreData]; [revoke reportSc OreWithCompletionHandler: ^ (NSError * error) {if (error! = Nil) {NSData * saveSocreData = [NSKeyedArchiver archivedDataWithRootObject: scoreReporter]; // If the score cannot be submitted, save the score and submit [self storeScoreForLater: saveSocreData]; else {NSLog (@ "submitted successfully") ;}}] ;}}
5. Display rankings
Create GKLocalboardViewController to display the ranking.
- (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. Score achievement
The challenge allows you to use GameCenter to challenge players in scoring or achievement. There are four challenges: "invalid", "pending", "ended", and "declined ".
-(Void) reportAchievment :( NSString *) identifier complete :( double) percentComplete {signature * achievement = [[GKAchievement alloc] identifier: identifier]; [achievement setPercentComplete: percentComplete]; [achievement reportAchievementWithCompletionHandler: ^ (NSError * error) {if (error! = Nil) {NSLog (@ "error: % @", [error localizedDescription]);} else {NSLog (@ "successful submission") ;}}];}
Example:
1. log on to GameCenter in the sandbox Environment
After successful logon, a banner "Welcome..." is displayed on the top. Click GameCenter to display information about you and your game.
3. Ranking page
4. Achievements
The achievement icon is configured in the background;
5. Add friends
Click the plus sign in the upper-right corner to display a page for adding friends. Enter the recipient's AppleID to send a friend request to the recipient.
6. Initiate challenges
Information Push interface when the recipient receives the challenge
Now, the access to gameCenter has come to an end. If there are any new discoveries in development, I will promptly share this article with you. You are welcome to comment and share your views.