I figured out how to do this, finally thanks to Felixyz . Below is what I need to do to store tabs, regardless of their data. If, say, data loaded from a URL is loaded in a view, save the URL instead of the entire view. You will have to redefine
- (void)encodeWithCoder:(NSCoder *)encoder
- (id)initWithCoder:(NSCoder *)decoder
UIViewController, , .
,
- (void)applicationWillTerminate:(UIApplication *)application
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeInt:tabBarController.selectedIndex forKey:@"TAB_INDEX"];
NSArray *keys = [NSArray arrayWithObjects:
@"NAVIGATION_CONTROLLER_1",
@"NAVIGATION_CONTROLLER_2",
@"NAVIGATION_CONTROLLER_3", nil];
for (int i = 0; i < keys.count; i++) {
UINavigationController *controller = [tabBarController.viewControllers objectAtIndex:i];
NSMutableArray *subControllers = [NSMutableArray arrayWithArray:controller.viewControllers];
[subControllers removeObjectAtIndex:0];
[archiver encodeObject:subControllers forKey:[keys objectAtIndex:i]];
}
[archiver finishEncoding];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[data writeToFile:[documentsDirectory stringByAppendingPathComponent:@"ARCHIVE_PATH"] atomically:YES];
}
,
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:
[[[UINavigationController alloc] initWithRootViewController:rootViewController1] autorelease],
[[[UINavigationController alloc] initWithRootViewController:rootViewController2] autorelease],
[[[UINavigationController alloc] initWithRootViewController:rootViewController3] autorelease], nil];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSData *archive = [NSData dataWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"ARCHIVE_PATH"]];
if (archive) {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:archive];
tabBarController.selectedIndex = [unarchiver decodeIntForKey:@"TAB_INDEX"];
NSArray *keys = [NSArray arrayWithObjects:
@"NAVIGATION_CONTROLLER_1",
@"NAVIGATION_CONTROLLER_2",
@"NAVIGATION_CONTROLLER_3", nil];
for (int i = 0; i < keys.count; i++) {
NSArray *controllers = [unarchiver decodeObjectForKey:[keys objectAtIndex:i]];
for (UIViewController *controller in controllers) {
[((UINavigationController *)[tabBarController.viewControllers objectAtIndex:i]) pushViewController:controller animated:NO];
}
}
}
[window addSubview:tabBarController.view];
}