views:

59

answers:

2

Hi everyone!

I need in a iPhone app to access files that the app is build with(.plist etc). There's an hardcoded way to do this:

NSString *appDir = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
                      objectAtIndex:0] 
                     stringByDeletingLastPathComponent]
                    stringByAppendingPathComponent:appFolder];

where appFolder is the name of folder app, like "test.app". After the appDir is known, to access files is simple.

Is there any other, not-hardcoded way to have access to files form the app?

Thanks in Advance!

+4  A: 
 NSString* pathToFile = 
    [[[NSBundle mainBundle] resourcePath] 
         stringByAppendingPathComponent:@"myFile.plist"];

// or, even better (handling localization):
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:@"myFile"
                                                       ofType:@"plist"];
Nikolai Ruhe
+1  A: 

Your app folder is the "main bundle". So you can use NSBundle methods such as

NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
KennyTM