How to load resource in cocoapods resource_bundle

前端 未结 8 1945
说谎
说谎 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 10:58

    Interesting thing is when you need to do it in the source files that are also in the cocoapod library I faced it when used a xib and plist files in my pod.

    That was solved in the next way. Example of loading a xib file:

    let bundle = Bundle(for: self.classForCoder)
    let viewController = CocoapodViewController(nibName: "CocoapodViewController", bundle: bundle)
    
    0 讨论(0)
  • 2020-12-01 11:01

    I don't think any of the other answers have described the relationship to the Podspec. The bundle URL uses the name of the pod and then .bundle to find the resource bundle. This approach works with asset catalogs for accessing pod images both inside and outside of the pod.

    Podspec

    Pod::Spec.new do |s|
      s.name             = 'PodName'
      s.resource_bundles = {
        'ResourceBundleName' => ['path/to/resources/*/**']
      }
    end
    

    Objective-C

    // grab bundle using `Class` in pod source (`self`, in this case)
    NSBundle *bundle = [NSBundle bundleForClass:self.classForCoder];
    NSURL *bundleURL = [[bundle resourceURL] URLByAppendingPathComponent:@"PodName.bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithURL:bundleURL];
    UIImage *podImage = [UIImage imageNamed:@"image_name" inBundle:resourceBundle compatibleWithTraitCollection:nil];
    

    Swift

    See this answer.

    0 讨论(0)
  • 2020-12-01 11:08

    It seems like for some reason **/*.{extensions} pattern is required for this to work I ended up creating my podspec like this

    s.resource_bundle = { '<BundleName>' => 'Pod/Resources/**/*.storyboard' }
    s.resource = 'Pod/Resources/**/*.storyboard'
    

    even though my actual path is Pod/Resources/.storyboard*

    And then to find my bundle I used @Jhelzer's answer although any way to get bundle e.g. by class or URL or bundleID will work after above setup.

    You can also have a look into this answer for more refrences.

    0 讨论(0)
  • 2020-12-01 11:13

    You can just check the ID of the Bundle by selecting the Pods project, selecting the desired target, and making sure you're on the General tab.

    Then in code you can load say an image in that Pod like so:

    backgroundImageView.image = UIImage(named: "business_fellas", in: Bundle(identifier: "org.cocoapods.StandardLibrary3-0"), compatibleWith: nil)
    
    0 讨论(0)
  • 2020-12-01 11:13

    Can create an extension to access the framework bundle easier in your framework source code

    extension Bundle {
       static func getResourcesBundle() -> Bundle? {
          let bundle = Bundle(for {your class}.self)
          guard let resourcesBundleUrl = bundle.resourceURL?.appendingPathComponent({bundle name}) else {
             return nil
          }
          return Bundle(url: resourcesBundleUrl)
       }
    }
    
    0 讨论(0)
  • 2020-12-01 11:16

    I struggled with a similar issue for a while. The resource bundle for a pod is actually a separate bundle from where your code lives. Try the following:

    let frameworkBundle = NSBundle(forClass: XDWebViewController.self)
    let bundleURL = frameworkBundle.resourceURL?.URLByAppendingPathComponent("XDCoreLib.bundle")
    let resourceBundle = NSBundle(URL: bundleURL!)
    let image = UIImage(named: "ic_arrow_back", inBundle: resourceBundle, compatibleWithTraitCollection: nil)
    print(image)
    
    0 讨论(0)
提交回复
热议问题