DEV Community

Cover image for How to add resources (xib, etc) to Static Library (iOS/Swift/Objc)
Deniss Kaibagarovs
Deniss Kaibagarovs

Posted on

How to add resources (xib, etc) to Static Library (iOS/Swift/Objc)

If you need to ship your static library that uses resources (images, xibs, fonts, etc) then you need to bundle them additionally. This article will guide you through that process.

  1. Select your Static Lib project and click create a New Target
    Alt Text

  2. Navigate to MacOS tab and choose Bundle, then finish the creation
    Alt Text

  3. Once the target is created, select it from the left-hand side menu, navigate to Build Settings, search for Supported Platforms and choose iOS
    Alt Text

  4. In the same place, search for Base SDK and choose iOS
    Alt Text

  5. Then navigate to your Static Library target and inside Build Phases tab add Static Lib Resources bundle that you just created to Dependencies
    Alt Text

  6. After that, navigate to your App Target that includes your Static Lib. Navigate to Build Phases of your app and add your Static Lib Resources bundle to Copy Bundle Resources
    Alt Text

  7. Now you could safely add any .xib, assets, or any other file to your Static Lib Resources bundle that you want to have access to in your Static Lib
    Alt Text
    Alt Text

Well done! Setup is completed, now we need to correctly ask for resources in code.

For that, please, refer to this code snippet that shows how to load a xib and an image from the resource bundle inside the static library.

Note: replace “StaticLibResources” with the name of your Static Lib Resources bundle

import UIKit

public class StaticLibViewController: UIViewController {

    @IBOutlet weak var iboImageView: UIImageView!

    public init() {
        let nibName = String(describing: type(of: self))
        super.init(nibName: nibName, bundle: Bundle.staticBundle)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public override func viewDidLoad() {
        super.viewDidLoad()
        self.iboImageView.image = UIImage(named: "image", in: Bundle.staticBundle, with: nil)
    }
}

extension Bundle {
    static func getBundle(bundleName: String) -> Bundle? {
        var resourceBundle: Bundle?
        for includedBundles in Bundle.allBundles {
            if let resourceBundlePath = includedBundles.path(forResource: bundleName, ofType: "bundle") {
                resourceBundle = Bundle(path: resourceBundlePath)
                break
            }
        }
        return resourceBundle
    }

    static let staticBundle = Bundle.getBundle(bundleName: "StaticLibResources")
}
Enter fullscreen mode Exit fullscreen mode

The whole project is available on GitHub: Link

If you encounter any error, feel free to contact me on Twitter and I’ll try to help you!

https://twitter.com/TheTaier

Also, feel free to follow if you want to stay up to date with new articles like this!

Top comments (0)