Saving and loading user data and preferences
There is a collectively recognized method for storing application data and preferences which is NSUserDefaults. The API for this is a bit unconventional and I thought an appropriate wrapper class would work well in this case. While not completely understanding the full details of NSUserDefaults I developed a singleton class which we can use in our games to save/load data. The interface is simple…
SettingsManager.h
#import <Foundation/Foundation.h> @interface SettingsManager : NSObject { NSMutableDictionary* settings; } -(NSString *)getString:(NSString*)value; -(int)getInt:(NSString*)value; -(void)setValue:(NSString*)value newString:(NSString *)aValue; -(void)setValue:(NSString*)value newInt:(int)aValue; -(void)save; -(void)load; -(void)logSettings; +(SettingsManager*)sharedSettingsManager; @end
SettingsManager.m
#import "SettingsManager.h" @implementation SettingsManager static SettingsManager* _sharedSettingsManager = nil; -(NSString *)getString:(NSString*)value { return [settings objectForKey:value]; } -(int)getInt:(NSString*)value { return [[settings objectForKey:value] intValue]; } -(void)setValue:(NSString*)value newString:(NSString *)aValue { [settings setObject:aValue forKey:value]; } -(void)setValue:(NSString*)value newInt:(int)aValue { [settings setObject:[NSString stringWithFormat:@"%i",aValue] forKey:value]; } -(void)save { // NOTE: You should be replace "MyAppName" with your own custom application string. // [[NSUserDefaults standardUserDefaults] setObject:settings forKey:@"MyAppName"]; [[NSUserDefaults standardUserDefaults] synchronize]; } -(void)load { // NOTE: You should be replace "MyAppName" with your own custom application string. // [settings addEntriesFromDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:@"MyAppName"]]; } // Logging function great for checkin out what keys/values you have at any given time // -(void)logSettings { for(NSString* item in [settings allKeys]) { NSLog(@"[SettingsManager KEY:%@ - VALUE:%@]", item, [settings valueForKey:item]); } } +(SettingsManager*)sharedSettingsManager { @synchronized([SettingsManager class]) { if (!_sharedSettingsManager) [[self alloc] init]; return _sharedSettingsManager; } return nil; } +(id)alloc { @synchronized([SettingsManager class]) { NSAssert(_sharedSettingsManager == nil, @"Attempted to allocate a second instance of a singleton."); _sharedSettingsManager = [[super alloc] init]; return _sharedSettingsManager; } return nil; } -(id)autorelease { return self; } -(id)init { settings = [[NSMutableDictionary alloc] initWithCapacity:5]; return [super init]; } @end
Values can then be saved/retrieved in code like so…
[[SettingsManager sharedSettingsManager] saveValue:@"hiscore" newValue:@"10000"]; NSString* myValue = [[SettingsManager sharedSettingsManager] getValue:@"hiscore"]; int myInt = [[SettingsManager sharedSettingsManager] getInt:@"challenges_won"];
To save our values on an incoming call or other system interruption we can put save/load calls in the appropriate callbacks of the app delegate…
- (void)applicationDidFinishLaunching:(UIApplication *)application { // ... (some code) ... // Load settings on initial startup // [[SettingsManager sharedSettingsManager] load]; // ... (more code) ... } - (void)applicationWillResignActive:(UIApplication *)application { [[SettingsManager sharedSettingsManager] save]; [[Director sharedDirector] pause]; } - (void)applicationDidBecomeActive:(UIApplication *)application { [[SettingsManager sharedSettingsManager] load]; [[Director sharedDirector] resume]; } - (void)applicationWillTerminate:(UIApplication *)application { [[SettingsManager sharedSettingsManager] save]; [[Director sharedDirector] end]; }
This basic class is great for storing arbitrary user state values that you want to preserve for the next time someone plays your game. Hi scores, level progression, number of lives are a few of the possibilities this class could be used for.
-
sir_seagill
-
bob
-
eric
-
Eric
-
Dashiell Gough
-
http://www.naveoss.com/ tebruno99







