结合SQLCipher的LKDBHelper数据库加密

    xiaoxiao2021-04-13  25

    在podfile文件中加入 pod 'FMDB/SQLCipher' #此处只能这样加入,分开(pod 'FMDB' pod'SQLCipher')加密无效。。。(亲试)至于原因暂时还不清楚,知道的同学希望能告知下,谢谢 pod 'LKDBHelper' 加密的时候 数据库不能有数据 LKDBHelper的作者已经提供了方法,只需要设置key就可以实现加密了 /** * @brief Set encryption key refer: FMDatabase.h - (BOOL)setKey:(NSString *)key; * invoking after the `LKDBHelper initialize` in YourModelClass.m `getUsingLKDBHelper` function */ - (BOOL)setKey:(NSString *)key; /// Reset encryption key - (BOOL)rekey:(NSString *)key; 在创建完 LKDBHelper 就要设置key //重载选择 使用的LKDBHelper +(LKDBHelper *)getUsingLKDBHelper { static LKDBHelper* db; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString* dbpath = [paths stringByAppendingPathComponent:[NSString stringWithFormat:@"userInfos/test.db"]]; NSLog(@"数据库路径------->%@",dbpath); db = [[LKDBHelper alloc] initWithDBPath:dbpath]; [db setKey:@"encryption"];//key可以是随意字符串 }); return db; }
    转载请注明原文地址: https://ju.6miu.com/read-668632.html

    最新回复(0)