12

I'm making an app that makes use of a framework that I'm creating.

The framework contains storyboards but in some cases the project making use of the framework will need to override the storyboard by providing a new one.

So my goal is to check if a storyboard with a given name exists in [NSBundle mainBundle], if not then I will get the base version from my framework.

I have tried getting the storyboard from the main bundle and checking for the result being nil but it throws an exception if it can't find it. So I then tried to catch the exception and then load the storyboard from the framework. This does work but it feels dirty and it could well hit the performance of my app.

I have also tried pathForResource in the bundle:

if([[NSBundle mainBundle] pathForResource:name ofType:@"storyboard"] != nil) {

}

But this never finds the storyboard.

Does anyone know of any other way that I can check if a storyboard exists in a specific bundle?

Swinny89
  • 7,273
  • 3
  • 32
  • 52
  • Since you cannot write into the application bundle at runtime you are supposed to know exactly what's in the bundle at compile time... – vadian Jul 26 '16 at 12:54
  • The code is in the framework. So i cannot tell from the framework what's in the main bundle that it's been embedded into.. – Swinny89 Jul 26 '16 at 12:56
  • Don't know if it will help, but a compiled storyboard has a file extension of "storyboardc". – Phillip Mills Jul 26 '16 at 13:22

2 Answers2

19

So, I was nearly there with what I was already trying. As per a comment on my question, the compiled extension for a storyboard is "storyboardc". So I can check whether or not a storyboard exists in a bundle like so:

if([[NSBundle mainBundle] pathForResource:name ofType:@"storyboardc"] != nil) {
    //The storyboard exists
} else {
    //The storyboard isn't in the bundle, get it from the framework bundle.
}
Swinny89
  • 7,273
  • 3
  • 32
  • 52
1

Swift:

if Bundle.main.path(forResource: storyboardName, ofType: "storyboardc") != nil 
{
   //The storyboard exists
}
  • 1
    Hello. This is a bit off-topic, and not optimal. The question is about Objective-C (look at the tags) so Swift is off-topic here. Also in Swift you should unwrap with `if let` instead of comparing to nil like in Objective-C. – Eric Aya Feb 11 '22 at 13:50