Rajiv Gaikwad
Rajiv Gaikwad

Reputation: 107

Copy Resource plist file to Application Support folder?

Is there any way to copy a plist file to the Application Support folder in iOS?.

The method below returns the Application Support foldery path, but I need to copy the resource plist file into that folder. Instead of using CopyItemAtUrl method and then delete from bundle.

- (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;
}

Upvotes: 0

Views: 219

Answers (1)

vadian
vadian

Reputation: 285079

The framework returns the correct path to the Application Support folder but unlike the Documents folder it's not being created by default.

Use the alternative API of NSFileManager which is able to create the folder implicitly

- (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];
}

Be aware that you are responsible for creating the sub folder, too.

- (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;
}

Upvotes: 1

Related Questions