Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode cannot find referenced Storyboard using Cocoapods

I'm trying to create a Pod using CocoaPods and I want to bundle a "Demo" Storyboard that I can reference to from the Main Storyboard from my example application. The problem is that Xcode gives me the following compiler error when I do so:

Did not find storyboard named "Demo" referenced from Main.storyboard

See:

enter image description here

enter image description here

In my Podspec, I included:

s.resource_bundles = {
    'StoryboardAssets' => ['Pod/Assets/*.{storyboard,png}']
}

You can find the demo repository that I created using the "Using Pod Lib Create" guide, you can find it here:

https://github.com/Kukiwon/StoryboardDemo

I'm running CocoaPods version 0.39.0.

So what would be the proper way to reference a Storyboard from your Pod? Am I missing something?

like image 433
Kukiwon Avatar asked Feb 19 '16 09:02

Kukiwon


2 Answers

Files included in a Dynamic Framework are embedded in a different NSBundle in the application. To make it easier to obtain the storyboard, you can create a helper class inside your library:

public class StoryboardHelper: NSObject {
    public static let helper = StoryboardHelper()

    public lazy var storyboard: UIStoryboard! = UIStoryboard(name: "Main", bundle: NSBundle(forClass: StoryboardHelper.self))

    public func rootController() -> UIViewController! {
        return storyboard.instantiateInitialViewController()
    }
}

NSBundle(forClass: StoryboardHelper.self) will obtain the bundle of StoryboardHelper.

This way, your user only needs to do the following to show your root view controller of the embedded storyboard:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    window?.rootViewController = StoryboardHelper.helper.rootController()
    window?.makeKeyAndVisible()

    return true
}
like image 116
redent84 Avatar answered Oct 03 '22 15:10

redent84


To reference storyboard using cocoapods, you should set in the Bundle section, the bundle identifier of the pod, like the picture below:

enter image description here

And in your podspec file, you should add the reference of your storyboard (as resource and not bundle)

s.resource = 'MyPod/MyStoryboardName.storyboard'

After the pod update, everything will work as expected

Hope that helps

like image 32
HamzaGhazouani Avatar answered Oct 03 '22 16:10

HamzaGhazouani