Use resource bundles in CocoaPods

我们两清 提交于 2020-03-19 19:36:36

问题


I am making a pod (MySDK) and would like to load the assets from the separate resource bundles CocoaPods generates.

However, I can not get it to work.

Here is how I tried to load the storyboard:

let storyBoard = UIStoryboard(name: "SDK", bundle: Bundle(identifier:"org.cocoapods.SchedJoulesSDK"))

This gives the error:

'Could not find a storyboard named 'SDK' in bundle

The bundle is added in Xcode:

And my podspec looks like this:

  s.resource_bundles = {
    'MySDK' => ['SDK/*/*.{xib,storyboard,xcassets}']
  }

Any ideas?


回答1:


You can use like...

s.resource  = "icon.png" //for single file

or

s.resources = "Resources/*.png" //for png file

or

s.resources = "Resources/**/*.{png,storyboard}" //for storyboard and png files

or

s.resource_bundles = {
   "<ResourceBundleName>" => ["path/to/resources/*/**"]
}



回答2:


If you use resource or resources in a CocoaPods PodSpec file, you tell Cocoapods that these are the resource files your library will load during runtime.

If your library is built as a dynamic framework, these files are just copied to the resource folder path of that framework and everything will be fine. Yet if your library is built as a static library, these are copied to the resource folder of the main application bundle (.app) and this can be a problem as this main application may already have a resource of that name or another Pod may have a resource of that name, in that case these files will overwrite each other. And whether a Pod is built as dynamic framework or as a static library is not specified by the PodSpec but in the Podfile by the application integrating your Pod.

Thus for Pods with resources, it is highly recommended to use resource_bundles instead!

In your case, the lines

s.resource_bundles = {
    'MySDK' => ['SDK/*/*.{xib,storyboard,xcassets}'] }

tell CocoaPods to create a resource bundle named MySDK (MySDK.bundle) and place all files matching the pattern into that resource bundle. If your Pod is built as a framework, this bundle is located in the resources folder of your framework bundle; if it is built as a static library, the bundle is copied to the resources folder of the main application bundle, which should be safe if you name your bundle the same way as your Pod (you should not name it "MySDK", rather "SchedJoulesSDK").

This bundle will have the same identifier as your Pod, however when dynamic frameworks are built, your framework bundle will have that identifier as well and then it's undefined behavior which bundle is being loaded when you load it by identifier (and currently the outer bundle always wins in my tests).

Correct code would look like this (not tested, though):

// Get the bundle containing the binary with the current class.
// If frameworks are used, this is the frameworks bundle (.framework),
// if static libraries are used, this is the main app bundle (.app).
let myBundle = Bundle(for: Self.self)

// Get the URL to the resource bundle within the bundle
// of the current class.
guard let resourceBundleURL = myBundle.url(
    forResource: "MySDK", withExtension: "bundle")
    else { fatalError("MySDK.bundle not found!") }

// Create a bundle object for the bundle found at that URL.
guard let resourceBundle = Bundle(url: resourceBundleURL)
    else { fatalError("Cannot access MySDK.bundle!") }

// Load your resources from this bundle.
let storyBoard = UIStoryboard(name: "SDK", bundle: resourceBundle)

As resourceBundle cannot change at runtime, it is safe to create it only once (e.g. on app start or when your framework is initialized) and store it into a global variable (or global class property), so you have it always around when needed (a bundle object also hardly uses any RAM memory, as it only encapsulates meta data):

final class SchedJoulesSDK {
    static let resourceBundle: Bundle = {
        let myBundle = Bundle(for: SchedJoulesSDK.self)

        guard let resourceBundleURL = myBundle.url(
            forResource: "MySDK", withExtension: "bundle")
            else { fatalError("MySDK.bundle not found!") }

        guard let resourceBundle = Bundle(url: resourceBundleURL)
            else { fatalError("Cannot access MySDK.bundle!") }

        return resourceBundle
    }()
}

The property is initialized lazy (that's default for static let properties, no need for the lazy keyword) and the system ensures that this happen only once, as a let property must not changed once initialized. Note that you cannot use Self.self in that context, you need to use the actual class name.

In your code you can now just use that bundle wherever needed:

let storyBoard = UIStoryboard(name: "SDK", 
    bundle: SchedJoulesSDK.resourceBundle)



回答3:


I have the same issue, if I only use the bundle line:

    s.resource_bundles = {
        'MySDK' => ['SDK/*/*.{xib,storyboard,xcassets}']
    }

I get a runtime crash when I reference my storyboard. However, if I explicitly add each storyboard as a resource like so:

s.resources = ["Resources/MyStoryboard.storyboard", "Resources/MyStoryboard2.storyboard"]

Everything works fine. I don't think we should have to explicitly add each storyboard as a resource, but I haven't been able to make it work any other way.

One thing you could try is changing you resource bundle reference to recursively search in your folders with the ** nomenclature like this:

s.resource_bundles = {
    'MySDK' => ['SDK/**/*.{xib,storyboard,xcassets}']
}


来源:https://stackoverflow.com/questions/50625036/use-resource-bundles-in-cocoapods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!