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

发布于 2024-11-07 10:00:21 字数 240 浏览 0 评论 0原文

我在 iPhone 应用程序中设置了 UIFileSharingEnabled。我想这样做,以便用户可以访问由核心数据管理的database.sqlite 文件。这样,他们就可以将其拖放到多部 iPhone/touch/iPad 之间,并将其用作穷人的同步。

但是,我不希望他们打开 sqlite 文件并 a) 修改数据库和 b) 对我的数据模型进行逆向工程。

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

I have UIFileSharingEnabled set in my iPhone app. I wanted to do this so the user could have access to the database.sqlite file managed by Core Data. This way, they could drag and drop it between their multiple iPhones/touchs/iPads and use it as sort of an poor-man's sync.

However, I don't want them opening the sqlite file and a) mucking around the database and b) reverse engineering my data model.

Does anyone know a way to password protect or lock out the database so that the user can't open it outside the app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路还长,别太狂 2024-11-14 10:00:21

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

当我将 iTunes 文件共享支持添加到我的应用程序时,我编写了代码以静默方式将 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;
}

You can filter out the files that you make visible to the app, but not what is visible to iTunes FS.

When I added my iTunes File Sharing support to my app, I wrote code to silently move the DB file to a new directory that would not be visible to iTFS. The code below moves the sqlite DB file from the Documents directory to the Application Support directory which is not visible to iTFS.

- (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;
}
人疚 2024-11-14 10:00:21

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

Maybe you can use some ZIP or RAR library as they have support for password protection. Then zip the sqlite file when your app moves to background and unzip when comes to foreground.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文