本文共 2123 字,大约阅读时间需要 7 分钟。
官方集成文档:https://www.zetetic.net/sqlcipher/ios-tutorial/
1> 选择你的项目,右键,选择 "Add Files to [你的工程]";在弹出的选择窗口找到你刚才从git下载的sqlcipher路径,打开sqlcipher文件夹,选择sqlcipher.xcodeproj
2> 点击工程,选择TARGETS中你的工程,点击 Build Phases Tab栏,展开 Target Dependencies 点击 +
4> 展开Link Binary With Libraries
注:如果你的工程库中已经添加了 libsqlite3.dylib 或者其他的SQLite库,请Remove掉,否则可能会提示出现重复sqlite库 6> 回到你的工程编辑面板,选择工程,TARGETS 你的工程Target, Build Settings Tab栏,选择 Header Search Paths 项,双击键入新值: ./sqlcipher/src
7> 选择 Other C Flags ,双击 添加 -DSQLITE_HAS_CODEC
@interface AppDelegate ()
@property (nonatomic) BOOL isLoginViewControllerDisplayed;
@property (readonly) NSURL *databaseURL;
@property (readonly) BOOL databaseExists;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Override point for customization after application launch.
// Set up a SQLCipher database connection:
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");
NSLog(@"Incorrect password!");
NSArray *URLs = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *directoryURL = [URLs firstObject];
NSURL *databaseURL = [directoryURL URLByAppendingPathComponent:@"secure.db"];
exists = [[self databaseURL] checkResourceIsReachableAndReturnError:&error];
if (exists == NO && error != nil)
NSLog(@"Error checking availability of database file: %@", error);