Hello,
Using Game Center in my app, I have to report scores, and to handle network errors.
I wrote some code but it doesn't work. And I have no info from the debugger.
I have a "HighScore.plist" file in my Resources panel, which is basically a NSDictionary with one key : "highScore", of Data type.
Here is my code :
Code:
-(void)reportScore:(int)score forCategory:(NSString *)category {
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error"
message:@"You are not connected to the Game Center, or you are not currently connected to the internet. Your score will be posted later."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
NSMutableDictionary *highScores = [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath]];
if (scoreReporter.value > [[highScores objectForKey:@"highScore"] intValue]) {
[highScores setObject:scoreReporter forKey:@"highScore"];
}
}
}];
}
The dataFilePath method :
Code:
-(NSString*)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"HighScore.plist"];
}
And, finally, in the app delegate, in the didFinishLaunchingWithOptions: method :
Code:
NSMutableDictionary *highScores = [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath]];
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:@"001"] autorelease];
scoreReporter.value = [[highScores objectForKey:@"highScore"]intValue];
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil) {
[highScores setObject:scoreReporter forKey:@"highScore"];
}
}];
(With the same dataFilePath method)
Do you have any ideas?
Thanks for your answers.