Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 574
Archie G. Quiñones Avatar asked Feb 09 '20 09:02

Archie G. Quiñones


People also ask

How do I add an asset in Xcode?

In the Project navigator, select an asset catalog: a file with a . xcassets file extension. Drag an image from the Finder to the outline view. A new image set appears in the outline view, and the image asset appears in a well in the detail area.

How do I open framework in Xcode?

One is to right click your "src" folder in Xcode, and select "New File..", the other is to go to the menu and select File > New > File... In the dialog that pops up, navigate to OS X > Source and then select C++ file.


1 Answers

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.

backup

like image 146
Asperi Avatar answered Nov 15 '22 05:11

Asperi