How can I instantiate a view controller from framework added to my OS X App (this may actually be the same process as iOS, I'm unsure, last time I looked Frameworks where not allowed on iOS but that may have changed)?
Here is the setup:
MyFramework.framework
,CustomViewController
,CustomViewController.xib
.What does not work
CustomViewController(nibName: "CustomViewController", bundle:nil)
will not work because the resources are not in the App's main bundle (they are in the framework).
What does work
To instantiate view controllers from the framework my App using the following code (it works!) which iterates through all loaded frameworks using NSBundle.allFrameworks()
and picks out my framework by comparing the bundleIdentifier
,
func findMyFrameworkBundle() -> NSBundle? {
if let frameworkBundles = NSBundle.allFrameworks() as? [NSBundle] {
for bundle in frameworkBundles {
if let identifier = bundle.bundleIdentifier {
if identifier == "com.Me.MyFramework" {
return bundle
}
}
}
}
return nil
}
let nibName = "CustomViewController"
let bundle = findMyFrameworkBundle()
let viewController = CustomViewController(nibName:nibName, bundle:bundle)
How to improve?
This seems overly complex which make me think that I must be doing something wrong. Some potential solutions:
For example,
class CustomViewController : NSViewController {
init() {
let bundle = // ... the correct bundle even when used as an embedded framework in an App
super.init(nibName: "CustomViewController", bundle: bundle)
}
// ... other code
}
NSBundle.allFrameworks()
?I may have misunderstood your question, but I think standard practice is to use the NSBundle
constructor that takes an identifier as its only argument. As in:
let bundle = NSBundle(identifier: "com.Me.MyFramework")!