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

4

37

I'm trying to make a framework that will bundle a storyboard. I have checked, and the created .framework includes my .storyboard file (as a .storyboardc file), and have gotten the framework to load the storyboard at runtime. However, this being a framework, I'd like for the code to be as versatile as possible, and I feel like my current solution is a little hacky. Currently, I'm loading the storyboard using the following code:

let mainBundlePath: String = NSBundle.mainBundle().resourcePath
let frameworkBundlePath = mainBundlePath.stringByAppendingPathComponent("Frameworks/AuthenticationManager.framework")
let frameworkBundle = NSBundle(path: frameworkBundlePath)
let storyboard = UIStoryboard(name: "Storyboard", bundle: frameworkBundle)

A couple of things I've noticed that could be gotchas:

  • The path of the framework (Frameworks/) could, potentially, change in the future, and should not relied upon?
  • The name of the framework could change, but without first getting the NSBundle of the framework there's no way of getting the product name?
  • Frameworks aren't really bundles (?), so loading them as a bundle could have unforeseen consequences in the future?

There may be other issues, solutions to the above issues, or the above issues may not be issues after all, but I've not been able to think of them.

This question may be be more suited for Code Review, but I felt it'd fit in well here, too. If it needs to be moved over there, feel free to do that or tell me to do it.

Adultery answered 27/7, 2014 at 16:14 Comment(1)
Hello, I know this topic is 4 years old.. I want to do pretty much same thing but I didn't find any tutorial or good example so I can learn from it. Can you (or anyone else) help me please ? just by giving me a hint or some links .. Thank you so muchStretcher
D
45

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)
Dominions answered 29/10, 2014 at 15:8 Comment(5)
Instead of let storyboardBundle = NSBundle(forClass: self), now it should be let storyboardBundle = NSBundle(forClass: className.self)Wayside
@azimov Well, it depends. If it's inside of a static method, self is enough. I added a little note to make things clearer. Thanks!Carlcarla
It's only a cleaner solution if this code is executing from within a class which is in the framework itself. If a "consumer" of the framework wanted access (and the access controls to the various classes were set to public), then the accepted answer is still the way to go.Trench
@Trench You could still use in its YourClass.self with a class from the framework, couldn't you?Neglect
This is not working for me. It's always crashingMatrilateral
S
61

A framework (created from the "Cocoa Touch Framework" template) has a bundle identifier, which is stored in the Info.plist of the generated framework bundle.

The bundle identifier is configured in the general target settings. By default, it is <organization prefix>.<framework name>.

You can locate the framework using this identifier, for example:

let frameworkBundle = NSBundle(identifier: "com.duffy.AuthenticationManager")
let storyboard = UIStoryboard(name: "Storyboard", bundle: frameworkBundle)
Sunburst answered 27/7, 2014 at 17:35 Comment(3)
Is there any way to access app's bundle from inside a framework. Using identifier might not be the feasible way as a framework might not know about app's bundle identifier.Marielamariele
@niks: Bundle.main is the main application bundle.Sunburst
My hero! you saved meConstructivism
D
45

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)
Dominions answered 29/10, 2014 at 15:8 Comment(5)
Instead of let storyboardBundle = NSBundle(forClass: self), now it should be let storyboardBundle = NSBundle(forClass: className.self)Wayside
@azimov Well, it depends. If it's inside of a static method, self is enough. I added a little note to make things clearer. Thanks!Carlcarla
It's only a cleaner solution if this code is executing from within a class which is in the framework itself. If a "consumer" of the framework wanted access (and the access controls to the various classes were set to public), then the accepted answer is still the way to go.Trench
@Trench You could still use in its YourClass.self with a class from the framework, couldn't you?Neglect
This is not working for me. It's always crashingMatrilateral
C
8

For Swift 3/4, it's now simply Bundle :

let bundle = Bundle(identifier: "com.myframework.id")
let storyboard = UIStoryboard(name: "FrameworkMain", bundle: bundle)
Cream answered 7/2, 2017 at 10:53 Comment(0)
B
6

This is a much better way to load the bundle in Swift 3 without the need of passing a string that is much more likely to fail

let myBundle = Bundle(for: MyViewController.self)
let myStoryboard = UIStoryboard(name: "StoryboardName", bundle: myBundle) 
Baumann answered 25/8, 2017 at 7:1 Comment(1)
If you have something like R.Swift, you can use the generated storyboard name to make it compile time safeBaumann

© 2022 - 2024 — McMap. All rights reserved.