首次启动时将文件安装到Application Support中

时间:2012-12-17 12:21:20

标签: cocoa default

我的应用程序允许用户创建预设并将其保存为Application support文件夹中的二进制文件。如果我的应用程序以一些预定义的预设开始,那将会很棒,但我无法弄清楚如何设置应用程序以在首次启动时安装此文件。

我是否要将此文件放入项目中,然后在首次启动时移动它们?这是一个好习惯吗?

1 个答案:

答案 0 :(得分:3)

我们在项目中这样做 - 我们检查应用程序开头是否存在文件。

    NSString* fileName = @"...";

    NSArray* dirs = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    dbPath = [[dirs objectAtIndex:0] stringByAppendingPathComponent:fileName];
    // check if DB exist in correct location
    NSFileManager* fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:dbPath];

    if(!success)
    {
        NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];

        [fileManager copyItemAtPath:path toPath:dbPath error:nil];

    }