Access Asset Catalog in Swift Package from Storyboard

Viewed 291

With the new Swift 5.3 package manager, it's possible for a Swift package to vend out images from an Asset catalog. Let's say I have a package called "ImageLibrary" which has image assets I wish to share between many projects.

Now I have my main app project and I import the Swift package. In my storyboard, XCode lets me select images from the asset catalog in my library "ImageLibrary". However, at runtime the images cannot be found, because they exist in a different bundle to the storyboard.

The only solution I can find is to drag the Image asset library from the "ImageLibrary" Swift package into my "Copy Bundle Resources" list. And this does works. The images end up in the app's bundle and appear at runtime.

The problem is that if my Swift package is referred to with a remote repository link, XCode will check the code out and compile it deep inside the Derived Data folders. And the "Copy Bundle Resources" link will point to that folder. And now the XCode project file has a link with information specific to my hard-disk buried in it. With a large team that won't work of course.

I could possibly use a local Swift package to deal with this, but I anticipate similar problems with continuous integration tools and so on. I suspect this is just something that was not intended to be done with Swift packages, and that I'm out of luck.

Thanks for any ideas, Simon.

1 Answers

One approach you can take is to declare public UIImages initialized with your graphics from the asset catalog all within the Swift package. Then the UIImage will be available to your main app project. This approach will work remote Swift packages as well as local Swift packages, so long as you are able to edit the Swift package source code yourself to update it.

Related