Access Resources in pod
Asked Answered
L

2

14

I would like to include image assets in a cocoapod library, but I'm having trouble accessing them. I've read these resources for help:

Cocoapods Resources

Cocoapods Resource Bundles

Mokacoding Resource Bundles

David Potter Resource Bundles

However, none point me in the direction of accessing the resources. I've tried the following:

[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle bundleWithIdentifier:@"Assets"] URLForResource:@"my-image@2x" withExtension:@"png"]]]
[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle bundleWithIdentifier:@"Assets"] URLForResource:@"my-image" withExtension:@"png"]]]
[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle bundleWithIdentifier:@"Assets.bundle"] URLForResource:@"my-image" withExtension:@"png"]]]
[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle mainBundle] URLForResource:@"my-image" withExtension:@"png"]]]
[[UIImage imageWithData:[NSData dataWithContentsOfURL:[(NSBundle *)[NSBundle mainBundle] URLForResource:@"my-image@2x" withExtension:@"png"]]]
[UIImage imageNamed:@"my-asset"]
[UIImage imageNamed:@"[email protected]"]

But none of them work.

I've setup my podspec with these alternatives and neither work with the above:

s.resources = ['Pod/Assets/*.{png, jpg}', 'Pod/Assets/**/*.{png, jpg}']
s.resource_bundles = {
    'Assets' => ['Pod/Assets/*.{png, jpg}', 'Pod/Assets/**/*.{png, jpg}']
}

The resources show up under 'Development Pods/My App/Resources' after a pod update, but I cannot access them for the life of me. There aren't any bundles and there isn't anything named 'Assets'.

Any help or guidance would be appreciated.

Lafave answered 12/9, 2014 at 22:24 Comment(2)
do you do both s.resources and s.resource_bundles or tried them separately? Perhaps there's some clash.Paramecium
@NiklasBerglund Both separately.Lafave
P
9

It depends on the version of Cocoapods that you use. With older versions of cocoapods, you could get away with using [NSBundle mainBundle]:pathForResource:ofType:

NSString *bundlePath = [[NSBundle mainBundle] 
    pathForResource:@"MyResourceBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

If you are using newer Cocoapods and using spec.resource_bundles instead of spec.resources in your podspec file, you have to avoid mainBundle and replace it with NSBundle bundleForClass, which "Returns the NSBundle object with which the specified class is associated"

Example:

NSString *bundlePath = [[NSBundle bundleForClass:[MyFrameworkClass class]] 
    pathForResource:@"MyResourceBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
Pedaias answered 31/8, 2015 at 16:14 Comment(2)
Thanks - I'll look into thisLafave
As an extension of this, I found that this doesn't work for loading images from asset catalogs located in a Cocapod. More info here: https://mcmap.net/q/604516/-load-image-from-cocoapods-resource-bundle I confirmed this with my colleagues who have also verified that there is no known way of doing this. So you have to use standalone image files and not asset catalogs.Pedaias
C
5

This tripped me up too. Maybe I'm misunderstanding +bundleWithIdentifier:, but I don't think you can use it to get to a resource bundle inside of your iOS app bundle. See the "Getting Bundles by Identifier" section of the bundle docs for more details.

What does work is +bundleWithPath:, e.g.:

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MyResourceBundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

I wrapped that inside of a dispatch_once block and call it any time I need to access this bundle.

I'm using the s.resource_bundles syntax in my podspec as well and this works fine with it.

Cristie answered 4/3, 2015 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.