Reputation: 3165
I'd like to make a folder which has a file in MyApp.app/Document folder by using NSFileManager. (MyApp is my custom app.)
So, I copied IMG_0525.jpg (For test) to project's folder.
And then trying to copy it from project's folder to MyApp.app/Document folder.
But I have no idea how to specify the path name. (source and destination path)
Would you let me know how?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self generateTableContents];
}
- (void)generateTableContents {
NSFileManager * fileManager = [NSFileManager defaultManager];
NSArray *appsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [appsDirectory objectAtIndex:0];
NSLog(@"documentPath : %@", documentPath);
[fileManager changeCurrentDirectoryPath:documentPath];
[fileManager createDirectoryAtPath:@"user_List1" withIntermediateDirectories:YES attributes:nil error:nil];
// I'm trying to copy IMG_0525.jpg to MyApp.app/Document/user_List1 folder.
[fileManager copyItemAtPath:<#(NSString *)srcPath#> toPath:<#(NSString *)dstPath#> error:<#(NSError * *)error#>];
}
Upvotes: 0
Views: 800
Reputation: 32681
Your code for getting this documents directory using NSSearchPathForDirectoriesInDomains
is correct, but note that this will not point into "MyApp.app/Documents". In fact, you can't modify the application's bundle content at runtime (btw it would violate the code signature of the bundle if you modify it), but you can copy files in the application's sandbox (which is outside of the "MyApp.app" bundle), and that's the path to this application's sandbox's Document folder that your call to NSSearchPathForDirectoriesInDomains
will return
That being said, you then now have the destination folder for your file, so that is the toPath:
parameter of -copyItemAtPath:toPath:error:
method. The only missing part is the source path that points to the resource in your bundle (to point to the image file you added in your Xcode project once it is compiled in the bundle).
To obtain this source path, use the -[NSBundle pathForResource:ofType:]
method. This is very simple to use:
NSString* sourcePath = [[NSBundle mainBundle] pathForResource:@"IMG_0525" ofType:"jpg"];
error:
parameter can be NULL
, or a pointer to an NSError*
object if you want to retrieve the error in case the -copyItemAtPath:toPath:error:
method failed. For that parameter, simply create an NSError* error;
variable before your call, and pass &error
to this third parameter of -copyItemAtPath:toPath:error:
.So the complete call will look like this:
NSError* error; // to hold the error details if things go wrong
NSString* sourcePath = [[NSBundle mainBundle] pathForResource:@"IMG_0525" ofType:"jpg"];
BOOL ok = [fileManager copyItemAtPath:sourcePath toPath: documentPath error:&error];
if (ok) {
NSLog(@"Copy complete!");
} else {
NSLog(@"Error while trying to copy image to the application's sandbox: %@", error);
}
Upvotes: 1