Game Development Reference
In-Depth Information
Listing 3-18. Sample_03AppDelegate.m (applicationDidEnterBackground:)
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSString* gameArchivePath = [self gameArchivePath];
[NSKeyedArchiver archiveRootObject:[gameController currentGame] toFile: gameArchivePath];
}
To archive our game state, Listing 3-18 shows that we again use the NSKeyedArchiver class to
archive our CoinsGame object, but this time we archive it to a file. The variable gameArchivePath
is the path to the file we are going to use as our archive. We get this path by calling the task
gameArchivePath , as shown in Listing 3-19.
Sample_03AppDelegate.m (gameArchivePath)
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentDirPath = [paths objectAtIndex:0];
return [documentDirPath stringByAppendingPathComponent:@"GameArchive"];
gameArchivePath task from Listing 3-19 shows that we use the function
and pass the NSDocumentDirectory , masked with the
. The YES at the end indicates that we want the tilde that is used to indicate the
a root directory to which files can be read and written. By getting the zero items out of the path's
NSArray , we get access to that directory. We simply specify the name of the file we want to use
passing an NSString to the stringByAppendingPathComponent task of documentDirPath .
We obviously need the path to the archive file when we unarchive the CoinsGame object as well.
Listing 3-20 shows where we unarchive this object.
Listing 3-20. Sample_03AppDelegate.m(application: didFinishLaunchingWithOptions:)
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString* gameArchivePath = [self gameArchivePath];
CoinsGame* existingGame;
@try {
existingGame = [[NSKeyedUnarchiver unarchiveObjectWithFile:gameArchivePath] retain];
}
@catch (NSException *exception) {
existingGame = nil;
}
[gameController setPreviousGame:existingGame];
[existingGame release];
Search WWH ::




Custom Search