Monday, April 29, 2013

NSBundle pathForResource example ios


pathForResource :ofType:inDirectory:

Returns the full pathname for the resource file identified by the specified name and extension and residing in a given bundle directory.
+ (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extensioninDirectory:(NSString *)bundlePath
Parameters of [NSBundle pathForResource]
name
The name of a resource file contained in the directory specified by bundlePath.
extension
If extension is an empty string or nil, the extension is assumed not to exist and the file is the first file encountered that exactly matches name.
bundlePath
The path of a top-level bundle directory. This must be a valid path. For example, to specify the bundle directory for a Mac app, you might specify the path /Applications/MyApp.app.
Return Value of [NSBundle pathForResource]
The full pathname for the resource file or nil if the file could not be located. This method also returns nil if the bundle specified by the bundlePath parameter does not exist or is not a readable directory.
Discussion of [NSBundle pathForResource]
The method first looks for a matching resource file in the non-localized resource directory of the specified bundle. (In OS X, this directory is typically called Resources but in iOS, it is the main bundle directory.) If a matching resource file is not found, it then looks in the top level of any available language-specific “.lproj” directories. (The search order for the language-specific directories corresponds to the user’s preferences.) It does not recurse through other subdirectories at any of these locations. For more details see Internationalization Programming Topics.


Example of [NSBundle pathForResource]
NSBundle *bundle = [NSBundle mainBundle];
NSLog(@"Resource path for test.applescript: %@", [bundle pathForResource:@"test" ofType:@"applescript"]);


Example of [NSBundle pathForResource]
NSString *images_misc_file = [[NSBundle mainBundle] pathForResource:@"mainmenu_background" ofType:@"png" inDirectory:@"images/misc"];