logo
Login

Sponsored Links

Sponsored

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.

FAQ

- 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];
  4.  
  5.         NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
  6.  
  7.         NSArray *dirContents = [fileManager contentsOfDirectoryAtPath: bundleRoot error: &error];
  8.         NSArray *onlyPdf = [dirContents filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"self ENDSWITH '.pdf'"]];
  9.  
  10.         debugLog(@"Sample PDF %@", onlyPdf);
  11.  
  12.  
  13.         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  14.         NSString *documentsDir = [paths objectAtIndex: 0];
  15.  
  16.         for (int i = 0; i < onlyPdf.count; i++) {
  17.                 NSString *pdfName = [onlyPdf objectAtIndex: i];
  18.  
  19.                 NSString *docPdfFilePath = [documentsDir stringByAppendingPathComponent: pdfName];
  20.  
  21.                 //Using NSFileManager we can perform many file system operations.
  22.                 BOOL success = [fileManager fileExistsAtPath: docPdfFilePath];
  23.  
  24.                 if (!success) {
  25.                         NSString *samplePdfFile  = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: pdfName];
  26.  
  27.                         success = [fileManager copyItemAtPath: samplePdfFile toPath: docPdfFilePath error: &error];
  28.  
  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. }

Like Developerfeed? Follows us via: RSS Feed Follow me on Twitter! Follow me on Facebook! Follow me on Youtube!

Socialize & Share Now :

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.

Enter your email address:

Subscribe & Follow

RSS Feed Follow me on Twitter! Follow me on Facebook! Follow me on Youtube!
Feed via Email:

Guide Navigation

Sponsored Links

Facebook

Sponsored Apps

Job Change Alert for Linkedin


Contact2CRM for Salesforce


Advertise on DeveloperFeed

About

Manpreet is an Architect & Software developer currently focusing on developing mobile apps on the iOS (iPhone/iPad) Platform. He’s the founder of a small iPhone development studio
called Livrona.

Subscribe to Developer Feed iPhone Blog

Tags

Who's online

There are currently 0 users and 1 guest online.