How to load resources from external framework

12,744

Solution 1

What you need to do is load the bundle for the framework, and then access the resources using the NSBundle object.

For example, if there is a framework that defines a class "FrameworkClass", we can do:

NSBundle *frameworkBundle = [NSBundle bundleForClass:[FrameworkClass class]];
NSString *resourcePath = [frameworkBundle pathForResource:@"an_image" ofType:@"jpeg"];
UIImage *image = [UIImage imageWithContentsOfFile:resourcePath];

That should more or less do what you want.

Solution 2

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.

Solution 3

Swift 3:

let bundle = Bundle(for: SomeFrameworkClass.self as AnyClass)

if let path = bundle.path(forResource: "test", ofType: "png") {
    if let image = UIImage(contentsOfFile: path) {
        // do stuff
    }
}

Solution 4

You can refer to Framework resources as follows :

[[NSBundle mainBundle] pathForResource:@"FI.framework/Resources/FileName"
                                ofType:@"fileExtension"];

Note, here FI is your framework name.

Ref. Link : http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/

Solution 5

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
Share:
12,744

Related videos on Youtube

topgun
Author by

topgun

Updated on September 16, 2022

Comments

  • topgun
    topgun almost 2 years

    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

  • topgun
    topgun almost 12 years
    I haven't copied the framework within the project but rather referenced it, would this work even without it? Does the framework gets added to the bundle at compile time? Just curious.
  • DShah
    DShah almost 12 years
    While creating framework, once you will reference it, it will be a part of your bundle. So, during building framework, all linked resources would be included in framework.
  • topgun
    topgun almost 12 years
    DShah, thanks for clearing that out. I was not referencing the framework within the resources that was the problem, now the problem got fixed. Thanks.
  • loretoparisi
    loretoparisi over 9 years
    This will not work in most of cases. Also it depends on the FI.framework location from the root folder of your app project.
  • Johnykutty
    Johnykutty over 9 years
    Hi, My doubt is, In my framework. The framework classes itself using some resources like xibs and images. How can I refer it from within the framework? Also one more doubt... my excecutable file itself has 50mb size, and if I add framework to copy resource will the app size increase that much?
  • Tamás Sengel
    Tamás Sengel over 2 years
    Important note: Make sure to point to a class, not a struct.