Skip to content

Instantly share code, notes, and snippets.

@billymeltdown
Created January 20, 2015 21:31
Show Gist options
  • Save billymeltdown/9842b9043e8a018d305a to your computer and use it in GitHub Desktop.
Save billymeltdown/9842b9043e8a018d305a to your computer and use it in GitHub Desktop.
Sample code cut from some messing with SecureLoginDelegate for a SQLCipher iOS tutorial
//
// AppDelegate.m
// SecureLoginDelegate
//
// Created by Billy Gray on 10/19/14.
// Copyright (c) 2014 Zetetic. All rights reserved.
//
#import "AppDelegate.h"
#import <sqlite3.h>
@interface AppDelegate ()
@property (nonatomic) BOOL isLoginViewControllerDisplayed;
@property (readonly) NSURL *databaseURL;
@property (readonly) BOOL databaseExists;
@end
@implementation AppDelegate
@dynamic databaseURL;
@dynamic databaseExists;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set up the window with loginViewController as the rootViewController for now
// to avoid showing app view on launch on iOS 8
[[self window] setRootViewController:self.loginViewController];
[[self window] makeKeyAndVisible];
self.isLoginViewControllerDisplayed = YES;
// Set up a SQLCipher database connection:
sqlite3 *db;
if (sqlite3_open([[self.databaseURL path] UTF8String], &db) == SQLITE_OK) {
const char* key = [@"StrongPassword" UTF8String];
sqlite3_key(db, key, (int)strlen(key));
if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
NSLog(@"Password is correct, or a new database has been initialized");
} else {
NSLog(@"Incorrect password!");
}
sqlite3_close(db);
}
return YES;
}
- (NSURL *)databaseURL {
NSArray *URLs = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *directoryURL = [URLs firstObject];
NSURL *databaseURL = [directoryURL URLByAppendingPathComponent:@"secure.db"];
return databaseURL;
}
- (BOOL)databaseExists {
BOOL exists = NO;
NSError *error = nil;
exists = [[self databaseURL] checkResourceIsReachableAndReturnError:&error];
if (exists == NO && error != nil) {
NSLog(@"Error checking availability of database file: %@", error);
}
return exists;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment