Uploading scores with GameKit
How do I save and upload scores later if there is no connection available? In a WWDC session it says to use the following code if no connection is available:
NSData *archivedScore = [NSKeyedArchiver archivedDataWithRootObject:[NSData dataWithBytes:&score length:sizeof(score)]];
开发者_运维技巧
I then save the NSData object to NSUSerDefaults. But how do I get an int score value back from that to report?
Thanks
Use NSKeyedUnarchiver
(from Archives and Serializations Programming Guide)
int score = 42;
NSData *archivedScore = [NSKeyedArchiver archivedDataWithRootObject:[NSData dataWithBytes:&score length:sizeof(score)]];
int *scorePtr = [[NSKeyedUnarchiver unarchiveObjectWithData:archivedScore] bytes];
NSLog(@"score = %d", *scorePtr); // Output: score = 42
精彩评论