Скопировать файл Resource plist в папку поддержки приложений? - PullRequest
0 голосов
/ 02 октября 2019

Есть ли способ скопировать файл plist в папку поддержки приложений в iOS?

Приведенный ниже метод возвращает путь к папке поддержки приложения, но мне нужно скопировать файл plist ресурса в эту папку. Вместо использования CopyItemAtUrl метод, а затем удалить из пакета.

- (NSURL*)applicationDataDirectory {

    NSFileManager* sharedFM = [NSFileManager defaultManager];

    NSArray* possibleURLs = [sharedFM URLsForDirectory:NSApplicationSupportDirectory
                                             inDomains:NSUserDomainMask];
    NSURL* appSupportDir = nil;
    NSURL* appDirectory = nil;

    if ([possibleURLs count] >= 1) {
        // Use the first directory (if multiple are returned)
        appSupportDir = [possibleURLs objectAtIndex:0];
    }

    // If a valid app support directory exists, add the
    // app's bundle ID to it to specify the final directory.

    if (appSupportDir) {
        NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
        appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];
    }

    return appDirectory;
}

1 Ответ

1 голос
/ 02 октября 2019

Платформа возвращает правильный путь к папке поддержки приложений, но в отличие от папки Documents она не создается по умолчанию.

Используйте альтернативный API NSFileManager, который способен неявно создавать папку

- (NSURL*)applicationDataDirectory {

    NSFileManager* sharedFM = [NSFileManager defaultManager];
    NSURL* appSupportDir = [sharedFM URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
    // add the app's bundle ID to it to specify the final directory.

    NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
    return [appSupportDir URLByAppendingPathComponent:appBundleID];
}

Имейте в виду, что вы также несете ответственность за создание подпапки.

- (NSURL*)applicationDataDirectory {

    NSFileManager* sharedFM = [NSFileManager defaultManager];
    NSURL* appSupportDir = [sharedFM URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
    // add the app's bundle ID to it to specify the final directory.

    NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
    NSURL* appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];
    if (![appDirectory checkResourceIsReachableAndReturnError:nil]) {
        [sharedFM createDirectoryAtURL:appDirectory withIntermediateDirectories:NO attributes:nil error:nil];
    }
    return appDirectory;
}
...