4.2用户登录 - (void) authenticateLocalPlayer { [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error){ if (error == nil) { //成功处理 NSLog(@"成功"); NSLog(@"1--alias--.%@",[GKLocalPlayer localPlayer].alias); NSLog(@"2--authenticated--.%d",[GKLocalPlayer localPlayer].authenticated); NSLog(@"3--isFriend--.%d",[GKLocalPlayer localPlayer].isFriend); NSLog(@"4--playerID--.%@",[GKLocalPlayer localPlayer].playerID); NSLog(@"5--underage--.%d",[GKLocalPlayer localPlayer].underage); }else { //错误处理 NSLog(@"失败 %@",error); } }]; }对于开发者来说,Game Center必须经过测试才能上线,没有上线的程序在测试环境中登录时会出现sandbox提示.如图. 4.3用户变更检测 5.对Leaderboard进行操作 当上传分数出错的时候,要将上传的分数存储起来,比如将SKScore存入一个NSArray中.等可以上传的时候再次尝试. 5.2下载一个分数 //GKScore objects provide the data your application needs to create a custom view. //Your application can use the score object’s playerID to load the player’s alias. //The value property holds the actual value you reported to Game Center. the formattedValue //property provides a string with the score value formatted according to the parameters //you provided in iTunes Connect. - (void) retrieveTopTenScores { GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init]; if (leaderboardRequest != nil) { leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal; leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime; leaderboardRequest.range = NSMakeRange(1,10); leaderboardRequest.category = @"TS_LB"; [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) { if (error != nil){ // handle the error. NSLog(@"下载失败"); } if (scores != nil){ // process the score information. NSLog(@"下载成功...."); NSArray *tempScore = [NSArray arrayWithArray:leaderboardRequest.scores]; for (GKScore *obj in tempScore) { NSLog(@" playerID : %@",obj.playerID); NSLog(@" category : %@",obj.category); NSLog(@" date : %@",obj.date); NSLog(@" formattedValue : %@",obj.formattedValue); NSLog(@" value : %d",obj.value); NSLog(@" rank : %d",obj.rank); NSLog(@"**************************************"); } } }]; } }说明: 5.3玩家信息交互 上面的friends得到的只是一个身份列表,里面存储的是NSString,想要转换成好友ID,必须调用- (void) loadPlayerData: (NSArray *) identifiers方法,该方法得到的array里面存储的才是GKPlayer对象.如下 /* Whether you received player identifiers by loading the identifiers for the local player’s friends, or from another Game Center class, you must retrieve the details about that player from Game Center. */ - (void) loadPlayerData: (NSArray *) identifiers { [GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) { if (error != nil) { // Handle the error. } if (players != nil) { NSLog(@"得到好友的alias成功"); GKPlayer *friend1 = [players objectAtIndex:0]; NSLog(@"friedns---alias---%@",friend1.alias); NSLog(@"friedns---isFriend---%d",friend1.isFriend); NSLog(@"friedns---playerID---%@",friend1.playerID); } }]; }至此,leaderboard功能介绍完毕 (责任编辑:admin) |