How to show my cocoa touch framework storyboard screen?

You need to load the view controller by instantiating it from the storyboard in the framework.

Here's how. First some initial conditions:

  • Let's say your framework is called Coolness.

  • Let's say your framework's storyboard is called CoolnessStoryboard.storyboard.

  • Let's say your framework has a public class called CoolnessViewController.

  • Let's say that CoolnessStoryboard has a scene whose view controller is CoolnessViewController and that this is its initial view controller.

Then in your main code you would import Coolness and, to present your CoolnessViewController from the storyboard, you would say:

let s = UIStoryboard (
    name: "CoolnessStoryboard", bundle: NSBundle(forClass: CoolnessViewController.self)
)
let vc = s.instantiateInitialViewController() as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)

Note the strategy here. Work backwards from the goal. We need the instance of CoolnessViewController that is in the storyboard. To get that, we need a reference to that storyboard. To do that, we need to identify that storyboard. How? We can identify it by name and by the bundle that it is in. But how can we identify that bundle? We can identify the bundle by means of a class in that bundle. We have such a class, because we have imported the framework (import Coolness) and the class there is public (so we can speak of it).


I had the same problem. This is how I solved after a little research:

1 - I have a framework called "Messenger.framework"

2 - Inside it, I have a storyboard file called "Messenger.Storyboard"

3- My initial view controller is a UINavigationController with a rootViewController and my ChatController.

So this is how a present my framework's chat UIViewController:

- (void)presentChatController
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Messenger"
                                                         bundle:[NSBundle bundleForClass:ChatController.class]];

    ChatController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Chat"];
    [self.navigationController pushViewController:controller animated:YES];
}

I hope this helps you.


If your framework name is MyFramework, and storyboard name is Main, with a viewcontoller with storyboard id vc

if let urlString = NSBundle.mainBundle().pathForResource("MyFramework", ofType: "framework", inDirectory: "Frameworks") {
  let bundle = (NSBundle(URL: NSURL(fileURLWithPath: urlString)))
  let sb = UIStoryboard(name: "Main", bundle: bundle)
  let vc = sb.instantiateViewControllerWithIdentifier("vc")
  self.showViewController(vc, sender: nil)
}

I have inculded the framework using cocoapods.