Getting bundle file references / paths at app launch
Solution 1
Or in Swift 4:
let url = Bundle.main.url(forResource: "FileName", withExtension: ".xyz")
Solution 2
Yes, you would get their path:
NSString *path = [NSBundle mainBundle] pathForResource:@"file1" ofType:@"png"];
NSURL *fileURL = [NSURL fileURLWithPath:path]
// save it as any other object or in a dictionary:
[myMutableDictionary setObject:fileURL forKey:@"file1.png"];
EDIT: to get the complete list of files, use the NSFileManager, get the path to the bundle itself, then walk each directory getting the files, making URLs, and saving them somewhere. There is oodles of code on SO how to walk a directory to do this. [You should update your question to be more specific on what you want, this was not made clear at all originally]
Solution 3
There's an API on NSBundle
(documentation) available on 10.6 or iOS 4 to get an NSURL
directly, rather than passing a path to the NSURL
constructor:
- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext;
It also has variants that take a subdirectory
and localizationName
, the same as its -pathForResource:
equivalents.
Andrew Lauer Barinov
Objective-C and Swift. I do a little Ruby and Clojure too.
Updated on March 18, 2020Comments
-
Andrew Lauer Barinov about 4 years
Suppose I have an arbitrary set of files included in the Main App Bundle. I would like to fetch the file URLs for those at launch and store them somewhere. Is this possible using
NSFileManager
? The documentation is unclear in that regard.Note: I only need the file URLs, I do not need to access the actual files.