WebOrCode WebOrCode - 6 months ago 31
iOS Question

Move file from app bundle to Document folder on ios

I have file in my app bundle on ios.

How can I move it to be in Document folder ?

Can this be done through some configuration or I need to make custom code for it ?

By custom code I mean:

check is this first run of app
if so:
move file to Document folder

Answer

Please check out NSFileManager which allows you to copy files within the file system as required. You should be aware that Apple will reject your app if the copied file is not flagged to NOT backup to iCloud.

I use the following methods to copy a sqlite database from the bundle to a folder; it should work for your purpose with minor modification.

+ (void)copyBundleFileToStoresDirectory:(NSString *)filename
{
    NSError *error;
    NSURL *fileURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:filename ofType:nil]];
    NSURL *pathURL = [SVOFileSystemMethods documentsSubdirectory:@"Stores" skipBackup:YES];

    if (fileURL)
    {
        if([[NSFileManager defaultManager] copyItemAtURL:fileURL toURL:pathURL error:&error])
        {
            //           NSLog(@"File successfully copied");
        }
        else
        {
            [[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"error", nil) message: NSLocalizedString(@"Failed to copy database from bundle.", nil)
                                      delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil)  otherButtonTitles:nil] show];
            NSLog(@"Error description-%@ \n", [error localizedDescription]);
            NSLog(@"Error reason-%@", [error localizedFailureReason]);
        }
    }
}


+ (NSURL *) documentsSubdirectory:(NSString *)subdirectoryName skipBackup:(BOOL) skipBackup
{
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:subdirectoryName];  //untested change here -- was literal @"/Stores"

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error]; //Create folder
        NSURL *finalURL = [NSURL fileURLWithPath:dataPath];
        if (skipBackup)
            [SVOFileSystemMethods addSkipBackupAttributeToItemAtURL:finalURL];      // flag to exclude from backups.
        return finalURL;
    }
    else
        return [NSURL fileURLWithPath:dataPath]; // already existed
}


//
//  flags URL to exclude from backup
//
+ (BOOL) addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success)
    {
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }

    return success;
}