iPhone : Unable to copy from MainBundle to NSDocumentDirectory - iphone

I am trying to copy a couple of image files from my MainBundle/SampleImages folder to the application NSDocumentDirectory/Library folder, but am unable to do so. I have checked that the images are in .app/Mainbundle/SampleImages directory and I have also checked that the Library folder is being created in the NSDocumentsDirectory, but no files are copied.
I have tried two approaches, given below, but with no success.
here is the code that I am using to copy files.
First Method
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:#"Library"];
NSString *resourceSampleImagesFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"SampleImages"];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];
[[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath
contents:mainBundleFile
attributes:nil];
Second Method
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentSampleImagesFolderPath = [documentsDirectory stringByAppendingPathComponent:#"Library"];
NSString *resourceSampleImagesFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"SampleImages/"];
[fileManager createDirectoryAtPath: documentSampleImagesFolderPath attributes:nil];
if([fileManager copyItemAtPath:resourceSampleImagesFolderPath toPath:documentSampleImagesFolderPath error:&error]) {
NSLog(#"success");
}
else {
NSLog(#"Failure");
NSLog(#"%d",error);
}
I have spent a couple of hours looking for a solution, any help here would be appreciated greatly.
Thank You
Shumais Ul Haq

I got it done with the second method by changing it bit.
There is no need for creating the directory as the SDK docs say that the destination directory must not exist.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:#"Library"];
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"SampleImages"];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];

Related

[NSBundle mainBundle]pathForResource returns nil

I'm trying to connect my app to SQLite database but I get messages that tell me that the database is created but there are no tables.
I debugged the program and discovered that
[[NSBundle mainBundle] pathForResource] always returns nil
NSString* file = [[NSBundle mainBundle]pathForResource:_databaseName ofType:_databaseType];
Why is this happening?
Please help me!
First : make sure your database is in the app bundle.
Then you need to implement methods to copy the path to the documentary Directory .as these
-(NSString *) getDBPath:(NSString*)databaseName {
// search for the existed paths
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:databaseName];
}
- (void) InstantiateYourDatabase:(NSString *)DatabaseName {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dpname=DatabaseName;
_dbPath = [self getDBPath:dpname];
BOOL success = [fileManager fileExistsAtPath:_dbPath];
NSLog(#"success == %i",success);
if( !success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:DatabaseName];
success = [fileManager copyItemAtPath:defaultDBPath toPath:_dbPath error:&error];
}
}
Tell me if it helps!
try below code for find database path
NSString *databaseName = #"your_db_name.sql";
NSString *databasePath;
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
NSLog(#"Database Path is: %#",databasePath);
You want More Detail about Sqllite Database Connectivity in iOS try this tutorial
Welcome!
If you created a sqlite db file and drag it to your project in xcode3, it's in application document folder while its running.
NSString *databaseName = #"cartoon.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentDir stringByAppendingPathComponent:databaseName];
Whenever you are using the database you have to use the Documents Directory

Copy folder from Resource to Document in iphone sdk

i have one subfolder in Resources with name SketchImages which contains 300 PNG images, what i need to do is on the startup of my app copy this folder in document folder of iphone sdk, i am trying the following code with no luck
BOOL success;
NSFileManager *fileManager = [[NSFileManager defaultManager] init];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory
stringByAppendingPathComponent:#"SketchImages"];
success = [fileManager fileExistsAtPath:documentDBFolderPath];
if (success)
{
return;
}else
{
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"DB"];
//[fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath
error:&error];
}
any help will be highly appriciated
You use the wrong subfolder name:
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"SketchImages"];

Create copy of database in iPhone

Can any one help me for:
How can we create copy of database?
How can we open that database in iPhone?
How can we create folder in our application path?
How can we copy folder with files in our application path?
"For Creating the copy of Database..."
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *finalPath = [documentPath stringByAppendingPathComponent:#"test.sqlite"];
success = [fileManager fileExistsAtPath:finalPath];
if(success)
{
NSLog(#"Database Already Created.");
return;
}
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"test.sqlite"];
success = [fileManager copyItemAtPath:defaultPath toPath:finalPath error:&error];
if(success)
{
NSLog(#"Database Created Successfully.");
}
"For Open that Database..."
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *finalPath = [documentPath stringByAppendingPathComponent:#"test.sqlite"];
if(sqlite3_open([finalPath UTF8String], &database) != SQLITE_OK)
{
sqlite3_close(database);
NSLog(#"Error to Open Database :- %s",sqlite3_errmsg(database));
}
"For Creating Folder in Application Path"
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/FolderName"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
}
"For Copying The Files And Folder in Application"
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/FolderName"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
BOOL successs;
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Files/FolderName"];
successs = [[NSFileManager defaultManager] fileExistsAtPath:defaultPath];
if(successs)
{
NSLog(#"TRUE");
NSString *strFile = [NSString stringWithFormat:#"%#",dataPath];
NSLog(#"File :- '%#'",strFile);
[fileManager copyItemAtPath:defaultPath toPath:strFile error:&error];
}
else
NSLog(#"FALSE");
}

Creating a plist file only on first runnable in the device's documents directory

I current have everything setup to read from the documents directory and write to it , but Cannot do it because the file doesnt exist yet.
How is a file created within the code?
If you already have a template version of the document in your application's bundle then you should be able to write it to the application's document directory using something similar to the following. I haven't tested this code so I've probably got a few things wrong but that's the general idea.
- (void) applicationDidFinishLaunching {
NSArray* directories = NSSearchPathsForDirectoriesInDomain(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [directories objectAtIndex: 0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: #"Something.plist"];
if (![[NSFileManager sharedInstance] fileExistsAtPath: path]) {
NSString* infoTemplatePath = [[NSBundle mainBundle] pathForResource: #"Something" ofType: #"plist"];
NSDictionary* info = [NSDictionary dictionaryWithContentsOfFile: infoTemplatePath];
[info writeToFile: path];
}
}
Use NSFileManger's fileExistsAtPath: to see if the file exist. If not create it before going on to the code that requires the file.
This one works better for me:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:#"*.db"];
if (![fileManager fileExistsAtPath:documentDBFolderPath])
{
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"*.db"];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
}

[iPhone Xcode]How can I get path to resource on simulator/device?

How can I get path to my resources on simulator and device?
I have this code:
[code]
NSString *tytul = [NSString stringWithUTF8String: tytul_c];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:tytul];
const char *sciezka;
if (![[NSFileManager defaultManager] fileExistsAtPath:tytul])
{
NSString *stringWithoutTXT = [tytul stringByReplacingOccurrencesOfString:#".txt" withString:#""];
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:stringWithoutTXT ofType:#"txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(#"%#",myPathDocs);
//sciezka = [myPathDocs UTF8String];
sciezka = [myPathInfo UTF8String];
return sciezka;
}
[/code]
And it works fine on the device but NOT on the simulator. How can I fix it?
And where should I put my resources? Now, I put them in Documents, in project folder. Is it ok?
You're somehow mixing resources and documents/tempfiles.
A Resource is a file, that you add to your XCode Project and that is listed in your targets "copy bundle resources" build phase. There are no subfolders in resources, but you can add a bundle as resource, which in turn contains other resources. You get a NSString of the path like:
[[UIBundle mainBundle] pathForResource:#"filename" ofType:#"extension"];
[UIBundle bundleWithPath:[[UIBundle mainBundle] pathForResource:#"subbundle" ofType:#"bundle"] pathForResource:#"filename" ofType:#"extension"];
Documents, temporary files and caches are files that are written by your app at runtime. These are usually stored in one of the directories you access by NSSearchPathForDirectoriesInDomains.
Your code should give you the path after the first three lines:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:tytul];
if you want to check for the existence of this file, you should use myPathDocs and not tytul, which only contains the filename or relative Path.
if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
{
return [myPathDocs UTF8String];
}
return NULL;