How to copy a file from the Main Bundle into the Documents Directory in iOS

- by

You can do this either by using paths or NSURLs. Apple recommends using NSURLs, so here’s how it works.

In this example we’re copying a file called “Amy.png” which exists in the app’s main bundle.

// file URL in our bundle
NSURL *fileFromBundle = [[NSBundle mainBundle]URLForResource:@"Amy" withExtension:@"png"];
    
// Destination URL
NSURL *destinationURL = [[self applicationDocumentsDirectory]URLByAppendingPathComponent:@"Amy.png"];
    
// copy it over
[[NSFileManager defaultManager]copyItemAtURL:fileFromBundle toURL:destinationURL error:nil];


If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.