0

My app started life as an iPad-only app. Now I am converting it into a smaller version suitable for the iPhone. I am using the same codebase, and many of the controllers don't actually need to change much - just the views need to be remade to suit the smaller screen. This is not a universal app, as some of the features differ between iPad and iPhone versions, and the price points in the App Store are very different.

I have added separate target with the device family set to iPhone. The original target has this set to iPad.

I remade a bunch of nibs with the filenames following the format view~iphone.xib, and when I run the iPhone build on an iPhone, these are correctly loaded instead of the original iPad ones (which have filenames in the format view.xib).

The problem is when I run the iPhone build on an iPad. As you'd expect for an iPhone app, it runs in that little iPhone window (that can be doubled to 2x). But the nibs that are loaded are the iPad nibs which obviously don't fit or resize well for the iPhone sized display. I don't understand why iOS wouldn't automatically load the iPhone versions. If I test UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad, it returns false, so it knows it is running in iPhone mode!

Given that I can't stop customers from running the iPhone app on an iPad, I want to make it work correctly. I've tried a few things but they do not seem right:

  • Changing the code to [[MyViewController alloc] initWithNibName:@"MyView~iphone" bundle:nil]; will load the correct nib, but that means I would have to put conditional code around all of the locations where a nib name is specified - this is a big app so there are a lot, and this could be error-prone in the future when I or another developer forgets to do this in new code.
  • Renaming all of the ~iphone nibs to have identical filenames to the iPad versions (i.e. view.xib), moving them into a separate directory (so that they don't collide with the names of the originals) and re-adding them back to the project but only included in the iPhone target (and not the iPad target), and unticking the original iPad nibs from the iPhone target (but still included on the iPad target) will cause the correct nibs to be loaded at runtime (because the "wrong" nibs are no longer in the bundle for each build), regardless of the naming. Note that if I leave the filename as view~iphone.xib, then when the iPhone build runs on an iPad, there is an exception upon trying to load that nib: Could not load NIB in bundle. The identical filenames make this method pretty unpleasant and at first glance it's not obvious what's going on.
  • Renaming the original xib to view~ipad.xib (with the iPhone nib as view~iphone.xib) still causes the iPad nib to be loaded when running the iPhone build on the iPad.

There must be a better way than either of these? Why is the wrong (iPad) nib loaded when the device clearly knows it is running an iPhone app?

Rob B
  • 1,485
  • 12
  • 16

1 Answers1

0

You should remove MyView~iPad from iPhone target (click on nib file, then in right panel under Target Membership section uncheck the file.

Also uncheck iPhone nibs for iPad target. This will exclude nibs, so they will not be copied to app package. And you can use the same approach for resources to lower app size.

brigadir
  • 6,874
  • 6
  • 46
  • 81
  • Thanks for the quick answer - I edited my question to note that in the second method I mentioned, if I leave the filename as `view~iphone.xib` then I get an exception at runtime. So I can't just exclude some files from some builds - the filename has to lack the `~iphone` or the iPad won't load it (I suspect it is looking for `~ipad` then falling back to just `view.xib`. – Rob B Sep 18 '12 at 06:42