How to get resources path? - iphone

I run my app on Simulator in XCode and it works fine but when I try to run it on the device it doesn't work.
I figured out that it doesn't open some files.
(I don't know if it gets wrong path or maybe the files aren't copied)
Before I open my files I get their path like that:
const char* Pobierz_sciezke(const char *tytul_c)
{
NSString *tytul = [NSString stringWithUTF8String: tytul_c];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:tytul];
if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
{
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:#"tex_dane" ofType:#"txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
}
NSLog(#"%#",myPathDocs);
const char *sciezka;
sciezka = [myPathDocs UTF8String];
return sciezka;
}
Is this code ok?
Maybe I should add my resources in a different way? Change project settings?

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

Write .kml files in iOS

I would like to know how can i write to a file within Application. The thing im trying to do here is, i am generating a KML (using class XMLWriter from https://github.com/skjolber/xswi). I can log the KML, everythings fine, now what i have to do is, Same this KML to a .kml file, and then email it to some address.
Emailing is no problem, i wanted to know how to write this KML into a .kml file, and then retrieve it for emailing. Can someone help me with a code snippet.
This tutorial did it, more then help.. The Code for generating the .kml file:
NSFileManager *filemgr;
NSData *databuffer;
NSString *dataFile;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
dataFile = [docsDir stringByAppendingPathComponent: #"datafile.kml"];
databuffer = [[xmlWriter toString] dataUsingEncoding: NSASCIIStringEncoding];
[filemgr createFileAtPath: dataFile contents: databuffer attributes:nil];
then to retrieve it i used:
NSFileManager *filemgr;
NSString *dataFile;
NSData *databuffer;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Identify the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
dataFile = [docsDir stringByAppendingPathComponent: #"datafile.kml"];
databuffer = [filemgr contentsAtPath: dataFile];
I believe you've got all your content into a string, if so you can proceed with saving it to .kml file with something like this:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fname = [tmp_FileName stringByAppendingFormat: #".kml" ];
NSString* filenameStr = [documentsDirectory
stringByAppendingPathComponent:fname];
[yourKMLString writeToFile: filenameStr atomically: YES];

iPhone : Unable to copy from MainBundle to NSDocumentDirectory

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];

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;