How to load resources from external framework

Viewed 12960

I have build an external framework, and I am trying to utilize some images which are in the resource directory of the framework. The framework is not being copied within the application but rather just referenced to it. How can I utilize UIImage accordingly from xyz.framework resource folder?

Thanks

5 Answers

Swift 4/5:

let bundle = Bundle(for: type(of: self))
// let bundle = Bundle(for: ClassOfInterest.self)) // or by pointing to class of interest
let path = bundle.path(forResource: "filename", ofType: "json")!
let url = URL(fileURLWithPath: path)
let data = try! Data(contentsOf: url)

This is from inside framework / unit tests, in production code you don't want to force try and force unwrap.

Use bundle identifier to locate your framework.

This requires the frameworks's "MACH-O Type" to be "Dynamic Library" and configured as "Embed & Sign".

NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.example.App.ExampleFramework"];
NSURL *url = [URLForResource:@"resource" withExtension:@"txt"];
// load the contents with this file url
Related