Loading a Resource (E.G. Storyboard) in a Swift Framework

Loading a resource (e.g. storyboard) in a Swift framework

Here's a cleaner solution (no framework identifiers involved):

let storyboardName = "StoryboardName"
let storyboardBundle = NSBundle(forClass: self)
let storyboard = UIStoryboard(name: storyboardName, bundle: storyboardBundle)

If this code is not inside of a static method, use YourClass.self instead of self.

Example for Swift 3:

let storyboardBundle = Bundle(for: NumberPadViewController.self)
super.init(nibName: "NumberPadViewController", bundle: storyboardBundle)

Go back to previous storyboard

I fixed it by getting the instance of a new view controller added on the user storyboard with the view controller identifier like so :

        ProcessOut.backViewController = storyboard?.instantiateViewControllerWithIdentifier("back")

And then I present it like I would present a normal viewController from my storyboard.

How to load resource in cocoapods resource_bundle

I struggled with a similar issue for a while. The resource bundle for a pod is actually a separate bundle from where your code lives. Try the following:

Swift 5

let frameworkBundle = Bundle(for: XDWebViewController.self)
let bundleURL = frameworkBundle.resourceURL?.appendingPathComponent("XDCoreLib.bundle")
let resourceBundle = Bundle(url: bundleURL!)

let image = UIImage(named: "ic_arrow_back", in: resourceBundle, compatibleWith: nil)
print(image)

-- ORIGINAL ANSWER --

let frameworkBundle = NSBundle(forClass: XDWebViewController.self)
let bundleURL = frameworkBundle.resourceURL?.URLByAppendingPathComponent("XDCoreLib.bundle")
let resourceBundle = NSBundle(URL: bundleURL!)
let image = UIImage(named: "ic_arrow_back", inBundle: resourceBundle, compatibleWithTraitCollection: nil)
print(image)

Framework iOS-XIB can't load NIB in package

I found a solution to this problem,
push the xib file to the resource folder

spec.resources     = "MobileAds/**/*.{png,jpeg,jpg,storyboard,xib,xcassets}"


Related Topics



Leave a reply



Submit