我正在创建一个需要存储多个不同级别的高分的应用程序。我正在使用http://www.raywenderlich.com/63235/how-to-save-your-game-data-tutorial-part-1-of-2上的优秀教程作为我的基础试图做。根据他们的建议,我使用了来自https://gist.github.com/dhoerl/1170641的 keychainwrapper
在我的 Xcode 外的 iOS sim 中,游戏状态保存工作得非常好,我可以关闭 Xcode,甚至重新启动我的 mac,高分就会显示出来。但是当我使用我的 iPhone 5 作为目的地时,它根本不起作用。如果我杀死该应用程序,高分就会消失。不确定我在这笔交易中缺少什么。
这是我的gameState.h:
#import <Foundation/Foundation.h>
@interface GameState : NSObject <NSCoding>
@property (assign, nonatomic) long score;
@property (assign, nonatomic) long highScoreL1;
@property (assign, nonatomic) long highScoreL2;
@property (nonatomic) long levelIndex;
@property (nonatomic) long lvlIndexMax;
+(instancetype)sharedGameData;
-(void)reset;
-(void)resetAll;
-(void)save;
@end
这是我的gameState.m
#import "GameState.h"
#import "KeychainWrapper.h"
@implementation GameState
static NSString* const SSGameDataChecksumKey = @"SSGameDataChecksumKey";
static NSString* const SSGameDataHighScoreL1Key = @"highScoreL1";
static NSString* const SSGameDataHighScoreL2Key = @"highScoreL2";
-(void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeDouble:self.highScoreL1 forKey:SSGameDataHighScoreL1Key];
[encoder encodeDouble:self.highScoreL2 forKey:SSGameDataHighScoreL2Key];
}
-(instancetype)initWithCoder:(NSCoder *)decoder {
self = [self init];
if (self) {
_highScoreL1 = [decoder decodeDoubleForKey:SSGameDataHighScoreL1Key];
_highScoreL2 = [decoder decodeDoubleForKey:SSGameDataHighScoreL2Key];
}
return self;
}
+(NSString*)filePath {
static NSString* filePath = nil;
if (!filePath) {
filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]stringByAppendingString:@"gamedata"];
}
return filePath;
}
+(instancetype)loadInstance {
NSData* decodedData = [NSData dataWithContentsOfFile: [GameState filePath]];
if (decodedData) {
NSString* checksumOfSavedFile = [KeychainWrapper computeSHA256DigestForData:decodedData];
NSString* checksumInKeychain = [KeychainWrapper keychainStringFromMatchingIdentifier:SSGameDataChecksumKey];
if ([checksumOfSavedFile isEqualToString: checksumInKeychain]){
GameState* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodedData];
return gameData;
}
}
return [[GameState alloc]init];
}
-(void)save {
NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: self];
[encodedData writeToFile:[GameState filePath] atomically:YES];
NSString* checksum = [KeychainWrapper computeSHA256DigestForData: encodedData];
if ([KeychainWrapper keychainStringFromMatchingIdentifier:SSGameDataChecksumKey]) {
[KeychainWrapper updateKeychainValue:checksum forIdentifier:SSGameDataChecksumKey];
} else {
[KeychainWrapper createKeychainValue:checksum forIdentifier:SSGameDataChecksumKey];
}
}
+(instancetype)sharedGameData {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [self loadInstance];
});
return sharedInstance;
}
-(void)reset {
self.score = 0;
}
-(void)resetAll {
self.score = 0;
//self.highScoreL1 = 0;
//self.highScoreL2 = 0;
}
@end
在每个级别中我跟踪分数,然后当用户输掉游戏时,我使用以下行来更新高分,然后进入 Game Over 场景:
[GameState sharedGameData].highScoreL1 = MAX([GameState sharedGameData].score, [GameState sharedGameData].highScoreL1);
最后,在 Game Over 中,我使用
[[GameState sharedGameData] save];
保存高分。
在此先感谢您的帮助,任何方向都会很棒。如果您需要更多信息,请告诉我!