启用iTunes文件共享时隐藏核心数据sqlite文件

Row*_*een 6 iphone cocoa core-data objective-c

我在我的应用程序中使用iTunes文件共享,并且需要将Core Data的sqlite数据库放在别处,以便用户不会使用它.我已经读过一篇关于隐藏Core Data使用的sqlite文件的最佳方法的SO帖子.

关于是否将数据库放在Library/Preferences或被调用的目录中似乎存在冲突的意见.data,但我认为我同意最好的方法是使用该.data目录.

目前有一种-applicationDocumentsDirectory方法由Core Data模板代码提供:

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
Run Code Online (Sandbox Code Playgroud)

我想实现一个调用的函数applicationHiddenDocumentsDirectory,它将允许我访问".data"子目录,但我不太了解Objective-C或Cocoa/Foundation框架来访问目录.

有人可以帮我实现这个方法吗?

谢谢!

== ==罗文

Mat*_*uch 14

你觉得这个怎么样?如果发生错误,您必须添加适当的操作.
编辑:我改变了这一点,因此数据库保存在库目录中,该目录由itunes备份,对用户不可见.Apple Q&A建议这样做

- (NSString *)applicationHiddenDocumentsDirectory {
    // NSString *path = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@".data"];
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [libraryPath stringByAppendingPathComponent:@"Private Documents"];

    BOOL isDirectory = NO;
    if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
        if (isDirectory)
            return path;
        else {
            // Handle error. ".data" is a file which should not be there...
            [NSException raise:@".data exists, and is a file" format:@"Path: %@", path];
            // NSError *error = nil;
            // if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
            //     [NSException raise:@"could not remove file" format:@"Path: %@", path];
            // }
        }
    }
    NSError *error = nil;
    if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) {
        // Handle error.
        [NSException raise:@"Failed creating directory" format:@"[%@], %@", path, error];
    }
    return path;
}
Run Code Online (Sandbox Code Playgroud)


Dir*_*nry 5

由于新的CoreData模板返回NSURL对象而不是NSString路径,因此这是我上面代码的更新版本:

- (NSURL *)applicationHiddenDocumentsDirectory {
  // NSString *path = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@".data"];
  NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
  NSString *path = [libraryPath stringByAppendingPathComponent:@"Private Documents"];
  NSURL *pathURL = [NSURL fileURLWithPath:path];

  BOOL isDirectory = NO;
  if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
    if (isDirectory) {
      return pathURL;
    } else {
      // Handle error. ".data" is a file which should not be there...
      [NSException raise:@"'Private Documents' exists, and is a file" format:@"Path: %@", path];
      // NSError *error = nil;
      // if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
      //     [NSException raise:@"could not remove file" format:@"Path: %@", path];
      // }
    }
  }
  NSError *error = nil;
  if (![[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) {
    // Handle error.
    [NSException raise:@"Failed creating directory" format:@"[%@], %@", path, error];
  }
  return pathURL;
}
Run Code Online (Sandbox Code Playgroud)