DesperateLearner
DesperateLearner

Reputation: 1175

NSMainbundle to NSDocumentsDirectory - Copying a PDF

I was trying to copy an existing pdf file in the NSMainBundle and into the NSDocumentsDirectory. This file is later loaded into the uiwebview. I can't seem to understand why it doesnt copy the file. Any help on this?

 NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *resourceDBFolderPath = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];


[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentsDirectory error:&error];



NSArray *path1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath =  [path1 objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:@"document.pdf"];
NSURL *url = [NSURL fileURLWithPath:fileLoc];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];

Upvotes: 0

Views: 680

Answers (2)

tiguero
tiguero

Reputation: 11537

Replace your code source with the following:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDirectoryPath = [documentsDirectory stringByAppendingPathComponent:@"document.pdf"];
NSString *resourceDBBundlePath = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];

[fileManager copyItemAtPath:resourceDBBundlePath toPath:documentDirectoryPath error:&error];

As mentioned by FD_ Your destination path need to include your file name.

Upvotes: 1

FD_
FD_

Reputation: 12919

Your destination path has to include the file name.

From the docs:

dstPath

The path at which to place the copy of srcPath. This path must include the name of the file or directory in its new location. This parameter must not be nil.

Upvotes: 2

Related Questions