从沙盒应用程序的应用程序包中复制文件

时间:2013-04-19 09:46:22

标签: macos bundle sandbox

我有一个带有一些预定义工具配置的图形应用程序。在应用程序的第一个午餐时,我想将这些默认配置文件从包内部复制到〜/ Library / Containers // Data / Library / ...中。怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个它会帮助你。

-(void)copy
{
    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSError *error;
    NSString *path = [self getPath];
    BOOL sucess = [filemanager fileExistsAtPath:path];

    if(!sucess){
        NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/*put your assets name with extension here */"];
        sucess = [filemanager copyItemAtPath:defaultPath toPath:path error:&error];
        if (!sucess) {
            NSAssert1(0, @"Failed '%@'.", [error localizedDescription]);
        }
    }
}
-(NSString *) getPath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"/*put your assets name with extension here */"];
}