Скопируйте файл из пакета приложения в папку поддержки пользователей. - PullRequest
2 голосов
/ 11 августа 2011

Я создаю приложение, в котором мне нужно скопировать определенный файл из комплекта приложения в папку поддержки пользователей.Вот код, который я использую на самом деле:

NSBundle *thisBundle = [NSBundle mainBundle];
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *userpath = [paths objectAtIndex:0];
userpath = [userpath stringByAppendingPathComponent:executableName];    // The file will go in this directory
NSString *saveFilePath = [userpath stringByAppendingPathComponent:@"db.sqlite"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
[fileManager copyItemAtPath:[thisBundle pathForResource:@"db" ofType:@"sqlite"] toPath:saveFilePath error:NULL];

Но это ничего не копирует, кстати, это приложение для Mac.

Ответы [ 2 ]

5 голосов
/ 11 августа 2011

Наконец-то все заработало с добавлением

if ([fileManager fileExistsAtPath:userpath] == NO)
    [fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:nil];

И полученный код:

NSBundle *thisBundle = [NSBundle mainBundle];

NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *userpath = [paths objectAtIndex:0];
userpath = [userpath stringByAppendingPathComponent:executableName];    // The file will go in this directory
NSString *saveFilePath = [userpath stringByAppendingPathComponent:@"db.sqlite"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:userpath] == NO)
    [fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:nil];

[fileManager copyItemAtPath:[thisBundle pathForResource:@"db" ofType:@"sqlite"] toPath:saveFilePath error:NULL];
0 голосов
/ 11 августа 2011

Вы можете дважды проверить пути к файлам:

NSLog(@"src: %@", [thisBundle pathForResource:@"db" ofType:@"sqlite"]);
NSLog(@"dst: %@", saveFilePath);
...