Reputation: 1619
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?
Upvotes: 0
Views: 6466
Reputation: 6767
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;
Upvotes: 3