How does one use resources such as Images, Xibs and Storyboards in static libs. By default this is easy to achieve in dynamic libs however when it comes to static libs things can get a bit challenging. In this article i hope to explain how you can uses resources such as a Storyboard and Images in Static libs.
Lets jump in.
Step 1: Creating the static library
Navigate to File -> New -> Project and select Cocoa Touch Static Library.
Lets give the static library a name. That was easy!
Step 2: Setting up Resource Bundle
Navigate to your library project and select Add Target. Then navigate to macOS and select Bundle.
Naming the resource bundle, i named it as Resources 🙃
Change your resource bundle base SDK setting to “Latest iOS…” since we are developing for iOS.
Finally add the resource as a target dependency to your static library.
Step 3: Setting up the resource
Setup a simple ViewController and Storyboard in your static library with all the appropriate configurations and linking. In order to get your Storyboard as a resource to the bundle all you have to do is add them to the “Copy Bundle Resources” build phase of the target .
Now your static library is ready to 🚀.
Step 4: Linking Static Library and Resource Bundle
Lets use our new Static library by create a simple single view application and presenting the “MyViewController:” modally on viewDidLoad. To achieve this you must drag and drop your static library .xcodeproj to your base application. This may look something like this.
You then link your static library through “Linked Frameworks and Library”, like so.
Add finally link the resource bundle from the static library to your base application. To do this we just need to drag it across from the navigator pane to the “Copy Bundle Resources” phase of the application project. This should look like this.
Step 5: Lets use it! 🎉
Add the following code to your view controller in your base application so that the Storyboard can be loaded from the appropriate resource bundle. Once the bundle is loaded you simple instantiate it and present it.
Piece of cake right!
What about images?
Yes you can introduce images to static libs but you must add your image asset to your resource bundle and load it through your Xib, Storyboard or via Bundle.
I hope your found this tutorial useful. If you have any question give me a ping and i would love to hear your thoughts on using resource bundles in static libs.