How to add resources (xib, etc) to Static Library (iOS/Swift/Objc)
Tags: swift, ios, static, library
Published: 2020-12-15 20:56:26

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](https://dev-to-uploads.s3.amazonaws.com/i/2wi6unlp4gshvygn50mq.png)



2. Navigate to **MacOS** tab and choose **Bundle**, then finish the creation
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/llytl3o258ne18psxhhz.png)



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](https://dev-to-uploads.s3.amazonaws.com/i/hvgaa6o6dr74pchn92g8.png)



4. In the same place, search for **Base SDK** and choose **iOS**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/votp3vul1eectm4cuizb.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/l6u6k0nsfof0b01pknbx.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/ulj7uatoo9vwz4xdbbl9.png)


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](https://dev-to-uploads.s3.amazonaws.com/i/r7f2zna5sz7ss8yyfogq.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vtzorv9vykafr4zkc1ew.png)

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")
}
```

The whole project is available on GitHub: [Link](https://github.com/taier/Static-Lib-With-Resources-Example)

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!