Getting bundle file references / paths at app launch
Asked Answered
E

4

30

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.

Eggplant answered 30/8, 2012 at 21:27 Comment(2)
What's a "file reference"? Path? URL? What?Capparidaceous
By file reference I mean the file URLEggplant
C
68

You can get the URL of a file in the main bundle using

NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeFile" ofType:@"jpeg"];
NSURL *url = [NSURL fileURLWithPath:path];

You can write this URL to, for example, a property list file in the Documents directory:

NSString *docsDir = [NSSearchForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [docsDir stringByAppendingPathComponent:@"Files.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[url absoluteString] forKey:@"SomeFile.jpeg"];
[dict writeToFile:plistPath atomically:YES];

If you don't know the names of the files and you just want to list all the files in the bundle, use

NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] bundlePath] error:NULL];
for (NSString *fileName in files) {
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:path];
    // do something with `url`
}
Capparidaceous answered 30/8, 2012 at 21:34 Comment(3)
What if I do not know the names of the files, I simply want to get all the files there?Eggplant
@AndrewLauerBarinov then you can read NSFileManager's docs. (But see the edit, anyway...)Capparidaceous
Thanks for the additional info!Eggplant
R
7

Or in Swift 4:

        let url = Bundle.main.url(forResource: "FileName", withExtension: ".xyz")
Rapport answered 17/3, 2018 at 10:25 Comment(2)
to use this, where should I save my file?? Do I also need to include the file to the project?Tankard
@Tankard your file needs to be added to the project and part of the target. All files that have the checkmark for the target will be available as part of the main bundle.Rapport
D
3

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]

Densmore answered 30/8, 2012 at 21:34 Comment(1)
What if I do not know the names of the files, I simply want to get all the files there?Eggplant
W
1

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.

Whittington answered 4/8, 2014 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.