Iphone:Get the File Path Which Is Within Subfolder of Resource Folder

iPhone : Get the file path which is within subfolder of Resource folder

To continue psychotiks answer a full example would look like this:

NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *filePath = nil;

if (filePath = [thisBundle pathForResource:@"Data" ofType:@"txt" inDirectory:@"Folder1"]) {

theContents = [[NSString alloc] initWithContentsOfFile:filePath];

// when completed, it is the developer's responsibility to release theContents

}

Notice that you can use -pathForResource:ofType:inDirectory to access ressources in sub directories.

Get Path to Subdirectory in Resources Folder

Just solved it.

NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *pngs = [mainBundle pathsForResourcesOfType:@".png"
inDirectory:@"random pictures"];

NSLog(@"pngs in my dir:%@", pngs);

dir structure: "Resources/random pictures".

This works, BUT, when you add the files to 'Resources', you need to check "Create folder references for any aded folders", and NOT "Create groups for any added folders".

important step

Cheers!

How to get path to a subfolder in main bundle?

In Swift-3 make URL, and call appendingPathComponent:

let resourcePath = Bundle.main.resourcePath
let subdir = URL(fileURLWithPath:resourcePath!).appendingPathComponent("sub").path

or simply

let subdir = Bundle.main.resourceURL!.appendingPathComponent("sub").path

(thanks, Martin R!)

See this Q&A on information on stringByAppendingPathComponent method in Swift.

How get list of folders and files from resource folder in iPhone?

You can get the path to the Resources directory like this,

NSString * resourcePath = [[NSBundle mainBundle] resourcePath];

Then append the Documents to the path,

NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"];

Then you can use any of the directory listing APIs of NSFileManager.

NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];

Get all URLs for resources in sub-directory in Swift

Consider that the yellow folders Sample Image are virtual groups, not real folders (although Xcode creates real folders in the project directory). All files in the yellow folders are moved into the Resources directory in the bundle when the app is built.

Real folders in the bundle are Sample Image in the project navigator.

Swift - How do I get the file path inside a folder

First make sure when you drag your folder audioFiles to your project to check copy items if needed and select create folder references. Make sure it shows a blue folder if your project.

Sample Image

Sample Image

Also NSBundle method pathForResource has an initialiser that you can specify in which directory your files are located:

let audioFileName = "audioName" 

if let audioFilePath = Bundle.main.path(forResource: audioFileName, ofType: "mp3", inDirectory: "audioFiles") {
print(audioFilePath)
}

If you would like to get that file URL you can use NSBundle method URLForResource(withExtension:, subdirectory:)

if let audioFileURL = Bundle.main.url(forResource: audioFileName, withExtension: "mp3", subdirectory: "audioFiles") {
print(audioFileURL)
}

How to access a list of all files in resource folder of my project?

This gets FileInfo's on all txt files in the resources:

        var fileInfos = NSBundle.GetPathsForResources(".txt", path)
.Select(a => new FileInfo(a));

Now you have the short name, full name etc to play with:

        foreach (var fileInfo in fileInfos)
{
System.Diagnostics.Debug.WriteLine(fileInfo.Name);

using (var streamReader = new StreamReader(new FileStream(fileInfo.FullName, FileMode.Open)))
{
System.Diagnostics.Debug.WriteLine(streamReader.ReadToEnd());
}
}

Cannot read file from project subfolder

Make sure your default.json file exists in Copy Bundle Resources in Build Phases of your app Target.

then you can access it using this, no need of subdir.

if let path = Bundle.main.path(forResource: "default", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path))
} catch {
print(error.localizedDescription)
}
}

Where do resource files go during Swift development?

Bundle resources are located in /Contents/Resources and can be included in subdirectories as well. The function .path(forResource:) automatically finds it for you.

As long as your file is drag-dropped in your Xcode project, a Build Phase entry will be added that automatically copies your file to the bundle Resource folder.



Related Topics



Leave a reply



Submit