Access Resources in pod

后端 未结 2 1759
遇见更好的自我
遇见更好的自我 2021-02-02 16:21

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

Coco

2条回答
  •  清歌不尽
    2021-02-02 17:04

    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];
    

提交回复
热议问题