繁体   English   中英

使用UIFileSharingEnabled在应用程序外部打开密码保护/锁定sqlite数据库?

[英]Password protect/lock sqlite database from being opened outside the app with UIFileSharingEnabled?

我的iPhone应用程序中设置了UIFileSharingEnabled。 我想这样做,以便用户可以访问由Core Data管理的database.sqlite文件。 这样,他们可以将其拖放到多个iPhone /触摸/ iPad之间,并将其用作穷人的同步设备。

但是,我不希望他们打开sqlite文件并a)破坏数据库,b)反向工程我的数据模型。

有谁知道一种密码保护或锁定数据库的方法,以使用户无法在应用程序外部打开它?

也许您可以使用一些ZIP或RAR库,因为它们支持密码保护。 然后,当您的应用程序移至后台时,压缩sqlite文件;到前景时,解压缩。

您可以过滤掉对应用程序可见的文件,但不能过滤iTunes FS可见的文件。

当我将iTunes File Sharing支持添加到我的应用程序时,我编写了代码以静默方式将DB文件移动到iTFS不可见的新目录。 下面的代码将sqlite DB文件从Documents目录移动到iTFS看不到的Application Support目录。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}

//
// Move th DB file to a different directory because of the file sharing
// feature.
//
NSString *oldDBFile = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"db.sqlite"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *newDBFile;
NSString *dbpath;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSError *error;
NSURL *storeUrl;
BOOL dir;

dbpath = ([paths count] > 0 ? [paths objectAtIndex:0] : @"");

newDBFile = [dbpath stringByAppendingPathComponent:@"db.sqlite"];

if ([dbpath length] &&
    [fileMgr fileExistsAtPath:dbpath 
                  isDirectory:&dir] == NO) {
    if ([fileMgr createDirectoryAtPath:dbpath
         withIntermediateDirectories:YES
                            attributes:nil error:&error] == NO) {
        NSLog(@"Cannot create %@: %@ %@",
              dbpath,
              [error localizedDescription],
              [error userInfo]);
    }
}

if ([fileMgr fileExistsAtPath:oldDBFile]) {
    if ([fileMgr moveItemAtPath:oldDBFile 
                         toPath:newDBFile 
                          error:&error] != YES) {
        NSLog(@"Error moving DB: %@: %@", [error localizedDescription], [error userInfo]);
        storeUrl = [NSURL fileURLWithPath:oldDBFile];
    } else {
        storeUrl = [NSURL fileURLWithPath:newDBFile];
    }
} else {
    storeUrl = [NSURL fileURLWithPath:newDBFile];
}

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];   

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: 
                              [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                              configuration:nil 
                                                        URL:storeUrl 
                                                    options:options 
                                                      error:&error]) 
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    // Handle the error.
}    

return persistentStoreCoordinator;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM