我正在尝试将 Game Center 实现到我的应用程序中,下面的代码正在报告分数。
我正在尝试完成一个解决方案,如果玩家未通过身份验证,它应该通过 scoreReporter 块将分数保存在 scoreDictionary 中。但是,当播放器未通过身份验证时,它永远不会遇到“If (error != nil)”语句。
事实上,它通过了整个街区。如果本地播放器通过身份验证,则执行该块。
这是我第一次详细研究游戏中心和街区,所以我有点迷失在这里。
我想要完成的是如上所述。
我使用 5.1 作为目标。
-(void)reportScore:(int64_t)score forCategory:(NSString*)category {
NSLog(@"reportScore: %lli", score);
NSLog(@"category: %@",category);
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:category];
scoreReporter.value = score;
NSLog(@"scoreReporter: %@", scoreReporter);
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
NSLog(@"Execute the scoreReporter the block");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *scoreFilePath = [NSString stringWithFormat:@"%@/scores.plist",[paths
objectAtIndex:0]];
NSMutableDictionary *scoreDictionary=[NSMutableDictionary
dictionaryWithContentsOfFile:scoreFilePath];
if (error != nil)
{
//There was an error so we need to save the score locally and resubmit later
NSLog(@"Saving score for later");
[scoreDictionary setValue:scoreReporter forKey:[NSDate date]];
[scoreDictionary writeToFile:scoreFilePath atomically:YES];
}
}];
}
播放器未通过身份验证时的 NSLog 输出:
reportScore: 80
category: com.xxxxxx.yyyyyyyHighScore
scoreReporter: <GKScore: 0xab611b0>player:(null) rank:0 date:2012-12-19 11:18:04 +0000 value:80 formattedValue:(null) context:0x0
播放器通过身份验证时的 NSLog 输出:
reportScore: 60
category: com.xxxxxx.yyyyyyyHighScore
scoreReporter: <GKScore: 0x964ce30>player:G:280817155 rank:0 date:2012-12-19 11:27:45 +0000 value:60 formattedValue:(null) context:0x0
Execute the scoreReporter the block