我正在创建一个需要存储多个不同级别的高分的应用。我在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模拟游戏中,游戏状态保存完美无缺,我可以关闭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];
保存高分。
提前感谢您的帮助,任何方向都会很精彩。如果您需要更多信息,请告诉我们!
答案 0 :(得分:0)
在不同场景之间保存本地数据的最安全和最快捷的方法是使用nsuserdefaults,例如
//静态变量
static NSString * topScore= @"topScore";
-(void)setTopscore:(long long)newScore
{
NSString *newhighScore = [NSString stringWithFormat:@"%lld", (long long)newScore];
[_userlocalData setObject:newhighScore forKey:topScore];
[_userlocalData synchronize];
}
//get top score from local database storage====================================================================//
-(NSString*)getTopscore
{
NSString *newhighScore = [_userlocalData stringForKey:topScore];
return newhighScore;
}
我尝试了它并且它在所有iOS设备和mac本身上没有任何问题