0

I'm working with a Pod that acts as a "Theming" pod for my other apps. I would like to use multiple xcassets files to have one xcasset by app.

For example:

  • The named color PrimaryColor in the App_1.xcasset would be blue
  • The same color named PrimaryColor in the App_2.xcasset would be red

So when I use UIColor(named: "PrimaryColor"), it loads the color from the right asset file.

I tried using bundles with the available init method of UIColor:

init?(named name: String, in bundle: Bundle?, compatibleWith traitCollection: UITraitCollection?) but the Bundle I created is always nil.

One bundle by app in the pod, with the colors assets inside:

enter image description here

I need to have the same color keys in the asset files but with different colors. This is why I need multiple asset files.

Then I use the following code according the app I use to get the right Bundle but it doesn't pass the first guard.

public enum Application: String {
    case app_1
    case app_2

    var colorsBundle: Bundle? {
        let bundleName  = rawValue.capitalized
        let type        = "bundle"

        guard let bundlePath = Bundle.main.path(forResource: bundleName, ofType: type) else {
            return nil
        }

        let colorsBundle = Bundle(path: bundlePath)
        return colorsBundle
    }
}
AnthonyR
  • 3,485
  • 1
  • 18
  • 41

1 Answers1

0

Finally get it thanks to this answer: https://stackoverflow.com/a/59667338/4733067.

I had to specify each bundle in the Podspec file:

  s.resource_bundles = {
      'App_1' => ['MyPodName/Assets/app_1.xcassets'],
      'App_2' => ['MyPodName/Assets/app_2.xcassets']
  }

Then use the following code to load the correct bundle according the current app value:

 var colorsBundle: Bundle? {
        let bundleName  = rawValue
        let type        = "bundle"

        guard let colorsBundleURL = Bundle(for: TheClassWhereIUseThisBundleVar.self).url(forResource: bundleName, withExtension: type) else {
            fatalError("\(bundleName).bundle not found!")
        }

        guard let colorsBundle = Bundle(url: colorsBundleURL) else { fatalError("Cannot access \(bundleName).bundle!") }

        return colorsBundle
}

And just have to store my xcasset files inside my pod in an Assets folder in my case.

AnthonyR
  • 3,485
  • 1
  • 18
  • 41