Need Help! Contact me.Email me Thank You.

Copy PDF Files from Main Bundle to Document Folder in iOS

Sprite Fading Effects

Based on the applications requirements or for demo purpose sometimes it is required to copy files from the Resource Directory to the some directory with the Application deployment root.  

For this we would scan for the files with the extension in this case pdf in the Main Bundle , then iterate over the list of filenames, compute the destination path and finally using the FileManager copy it over. The snippet below shows how to do so. It also checks if the destination file already exits, then it skips the copy.



- How do you get the FileManager?

NSFileManager *fileManager = [NSFileManager defaultManager];

How do you get the path of Main Bundle?

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];

How do you get the Directory Contents of Main Bundle?

NSArray *dirContents = [fileManager contentsOfDirectoryAtPath: bundleRoot error: &error];

How do you filter files of a given extension from Main Bundle?

  NSArray *onlyPdf = [dirContents filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @”self ENDSWITH ‘.pdf'”]];

How do you get the path of the Documents Directory?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex: 0];

Code Snippet

The following code snippet shows the main methods.

  1. + (void) copySampleFilesToMyDocumentsFolder {
  2.         NSError *error;
  3.         NSFileManager *fileManager = [NSFileManager defaultManager];
  5.         NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
  7.         NSArray *dirContents = [fileManager contentsOfDirectoryAtPath: bundleRoot error: &error];
  8.         NSArray *onlyPdf = [dirContents filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"self ENDSWITH ‘.pdf’"]];
  10.         debugLog(@"Sample PDF %@", onlyPdf);
  13.         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  14.         NSString *documentsDir = [paths objectAtIndex: 0];
  16.         for (int i = 0; i < onlyPdf.count; i++) {
  17.                 NSString *pdfName = [onlyPdf objectAtIndex: i];
  19.                 NSString *docPdfFilePath = [documentsDir stringByAppendingPathComponent: pdfName];
  21.                 //Using NSFileManager we can perform many file system operations.
  22.                 BOOL success = [fileManager fileExistsAtPath: docPdfFilePath];
  24.                 if (!success) {
  25.                         NSString *samplePdfFile  = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: pdfName];
  27.                         success = [fileManager copyItemAtPath: samplePdfFile toPath: docPdfFilePath error: &error];
  29.                         if (!success)
  30.                                 //              NSAssert1(0, @"Failed to copy file ‘%@’.", [error localizedDescription]);
  31.                                 debugLog(@"Failed to copy %@ file, error %@", pdfName, [error localizedDescription]);
  32.                         else {
  33.                                 debugLog(@"File copied %@ OK", pdfName);
  34.                         }
  35.                 }
  36.                 else {
  37.                         debugLog(@"File exits %@, skip copy", pdfName);
  38.                 }
  39.         }
  40. }
Subscribe to DeveloperFeedDid you like this article? Did it Help you Solve your Problem? You can get the all the latest articles published at DeveloperFeed in your email inbox by entering your email address below. Your address will only be used for mailing you the articles, and each one will include a link so you can unsubscribe at any time.