How to load resource in cocoapods resource_bundle

前端 未结 8 1946
说谎
说谎 2020-12-01 10:43

I have struggled a lot to how to load resource in cocoapods resource_bundle.

The following is what i put in the .podspecs file.

s.sourc         


        
相关标签:
8条回答
  • 2020-12-01 11:16

    Just for the record: I wanted to open a NIB from a Category stored in a CocoaPods library. Of course [self classForCoder] gave UIKit back (because of the Category), so the above methods didn't work.

    I resolved the issue using some hardcoded paths:

    NSURL *bundleURL = [[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:@"Frameworks/MyCocoaPodLibraryName.framework"];
    NSBundle *podBundle = [NSBundle bundleWithURL:bundleURL];
    
    CustomView *customView = [[podBundle loadNibNamed:@"CustomView" owner:self options:nil] firstObject];
    
    0 讨论(0)
  • 2020-12-01 11:17

    This will work

    In .podspec add s.resources in addition to s.resource_bundles (pod install afterwards)

    s.resources = 'XDCoreLib/Pod/Resources/**/*.{png,storyboard}'
    

    Then in your code, its as easy as:

    let image = UIImage(named: "image_name.png", in: Bundle(for: type(of: self)), compatibleWith: nil)
    

    Note: You may need to run pod install after updating the .podspec to have the changes to your Xcode proj

    0 讨论(0)
提交回复
热议问题