How to Get Path to a Subfolder in Main Bundle

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.

Get nil when looking for file in subdirectory of main bundle

The solution is to use

Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "games/game1")

I don't know why, but it doesn't work if we get a path, then construct an URL from it.

Note: You must

  1. Copy items if needed
  2. Create folder references (not "Create groups")
  3. Add to targets

Problem with get path to a subfolder in main bundle in AVPlayer

First of all you are responsible for creating the directory structure of the bundle so don't mess it up.

Secondly use the URL related API.

For example put all mp4 files in a subdirectory named videos. It's crucial that you create a real blue folder rather than a group (yellow) folder.

This would reduce your code to

var videoArray = [URL]()

override func viewDidLoad() {
super.viewDidLoad()

videoTableView.delegate = self
videoTableView.dataSource = self

videoView.addSubview(videoVC.view)
videoVC.view.frame = videoView.frame
videoVC.showsPlaybackControls = false

videoArray = Bundle.main.urls(forResourcesWithExtension: "mp4", subdirectory: "videos")!
playTopVideo(value: 0)
}

func playTopVideo(value: Int) {
let videoURL = videoArray[value]
videoVC.player = AVPlayer(url: videoURL)
videoVC.player?.play()
}

Getting a subdirectory from mainBundle in xcode

The yellow folder icon for your "Objects" folder indicates that you created a folder group instead of a folder reference (those have blue folder icons) when you dragged your image folder onto the Xcode project.

Xcode copies files in a folder group into the root directory of your app bundle during the "Copy Bundle Resources" build phase.

If you want a subdirectory named "Objects" in your app bundle, you have to choose "Create folder references for any added folder" after dragging "Objects" into your Xcode window.

You projects sidebar should display the "Objects" folder as follows:

Sample Image

To get a list of all .pngs from that copied folder, you can use the following code:

NSURL* resourceURL = [[NSBundle mainBundle] resourceURL];
resourceURL = [resourceURL URLByAppendingPathComponent:@"Objects"];
NSError* error = nil;
NSArray* resourceURLContents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:resourceURL includingPropertiesForKeys:nil options:0 error:&error];
resourceURLContents = [resourceURLContents filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSURL* evaluatedObject, NSDictionary *bindings) {
return [[evaluatedObject pathExtension] isEqualToString:@"png"];
}]];
NSLog(@"Contents:%@", resourceURLContents);

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.

iPhone SDK: subFolders inside the main bundle

To create the subfolders inside the .app bundle, you should check the option "Create folder references for any added folders" rather than the default "Recursively create groups for any added folders"
Now in XCode, your imported folder appears blue rather than yellow. Build and go and you should see folders in your .app file.

Objective C - Get file path in subfolder of supporting files

You need to use a different method so you can specify the path:

- (NSString *)pathForResource:(NSString *)name
ofType:(NSString *)extension
inDirectory:(NSString *)subpath

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.



Related Topics



Leave a reply



Submit