Skip to content

Instantly share code, notes, and snippets.

@ddemaree
Created December 10, 2008 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddemaree/34173 to your computer and use it in GitHub Desktop.
Save ddemaree/34173 to your computer and use it in GitHub Desktop.
#pragma mark Data file management stuff
- (void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *writableDBPath = [[GBAppDelegate documentsDirectory] stringByAppendingPathComponent:@"/database.sqlite3"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/database.sqlite3"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
//
// Santa_s_ListAppDelegate.h
// Santa's List
//
// Created by David Demaree on 11/6/08.
// Copyright Practical 2008. All rights reserved.
//
#import <UIKit/UIKit.h>
@class DataController;
@interface GBAppDelegate : NSObject <UIApplicationDelegate> {
NSUserDefaults *_defaults;
UIWindow *window;
UINavigationController *navigationController;
DataController *dataController;
}
@property (nonatomic, retain) NSUserDefaults *defaults;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) DataController *dataController;
+ (NSString*)documentsDirectory;
- (void)askAboutTheCode;
@end
//
// Santa_s_ListAppDelegate.m
// Santa's List
//
// Created by David Demaree on 11/6/08.
// Copyright Practical 2008. All rights reserved.
//
#import "GBShared.h"
#import "RootViewController.h"
#import "SecretCodeView.h"
@implementation GBAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize dataController;
@synthesize defaults=_defaults;
+ (NSString*)documentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[navigationController setNavigationBarHidden:NO];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.defaults = defaults;
[defaults release];
DataController *dataCon = [[DataController alloc] init];
self.dataController = dataCon;
[dataCon release];
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
- (void)askAboutTheCode {
// Have we asked about the secret code yet?
BOOL shouldResetSecretCode = [self.defaults boolForKey:@"resetSecretCode"];
BOOL hasAskedAboutSecretCode = [self.defaults boolForKey:@"hasAskedAboutSecretCode"];
NSString *secretCode = [self.defaults stringForKey:@"secretCode"];
if(shouldResetSecretCode) {
[self.defaults setBool:NO forKey:@"hasAskedAboutSecretCode"];
[self.defaults setBool:NO forKey:@"resetSecretCode"];
[self.defaults setObject:NULL forKey:@"secretCode"];
}
if(hasAskedAboutSecretCode && (secretCode == nil)){
NSLog(@"Has asked and code is blank, allow into the app %@", secretCode);
}
else {
NSLog(@"No, would you please ask about the secret code?");
SecretCodeView *scv = [[SecretCodeView alloc] initWithNibName:@"SecretCode" bundle:nil];
[navigationController presentModalViewController:scv animated:NO];
}
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"Application WILL terminate");
// Save data if appropriate
[dataController.peeps makeObjectsPerformSelector:@selector(dehydrate)];
[self.defaults synchronize];
}
- (void)applicationDidBecomeActive:(UIApplication*)app {
NSLog(@"App did become active");
[self askAboutTheCode];
}
- (void)dealloc {
[dataController release];
[navigationController release];
[window release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment