MrDeveloper
MrDeveloper

Reputation: 47

How to Copy file from document into bundle of app in iOS?

I want to copy file from document into resource folder in iOS.

Not resource to document.

File from Document to Resource.

So i wrote following codes.

- (NSString *) getDBPath2 
{   
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Htoo.mp3"];

    return dbPath;
}

- (void) copyDatabaseIfNeeded 
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(success) 
    {   
        NSString *defaultDBPath = [[NSBundle mainBundle] resourcePath];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
}

- (NSString *) getDBPath 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"Htoo.mp3"];
}

When i wrote above code , i got error message.

2012-08-17 23:55:03.482 TestProjects[1645:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to create writable database file with message 'The operation couldn’t be completed. (Cocoa error 516.)'.'

So how can i copy file from document into Resource in iOS?

Thanks you for your reading.

Upvotes: 3

Views: 1144

Answers (2)

Vibol
Vibol

Reputation: 1168

If you want to save MP3 then you'd better save it in Cache:

    NSString *desPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

Upvotes: 0

andreamazz
andreamazz

Reputation: 4286

The resources of your app (the Bundle) are read only, you can't modify your bundle after it has been published. The bundle package is created on compile time by Xcode, and you can't modify it on runtime. There are plenty of ways to store data once the app is installed: NSDefaults, sqlite, Core Data, documents directory.

Upvotes: 4

Related Questions