iosswiftios-frameworks

iOS: How to add Assets folder inside a Framework and how to access them in code?


I create an iOS app and added a framework to it. The generated framework doesn't have an assets folder like the generate Single View App. So I made an Assets folder inside the framework folder and drag and drop it to xcode, choose the target as my framework.

I tried using the asset but the asset doesn't show up. Can show one show me how to correctly do this? is it possible to create an assets folder inside a framework?

I am very new to iOS so any guidance would be much appreciated. Thanks in advance.


Solution

  • Add Asset catalog to framework target as usual via New File... > Resources, but to get resource from such asset it needs to specify bundle explicitly, as in below example...

    Assuming that ImageProvider is in framework target, the code could be

    public class ImageProvider {
        // convenient for specific image
        public static func picture() -> UIImage {
            return UIImage(named: "picture", in: Bundle(for: self), with: nil) ?? UIImage()
        }
    
        // for any image located in bundle where this class has built
        public static func image(named: String) -> UIImage? {
            return UIImage(named: named, in: Bundle(for: self), with: nil)
        }
    }
    

    of course you can name such class anyhow.