views:

39

answers:

2

Here is my question :

I have an app running smoothly on iOS 3.0 I've been working a long time to port it to 4.0 and to include the new features.

I added iAds using Interface Builder.

I know that I have to define it programmatically to still support 3.0 devices. I weak linked the framework. But the app crashes when loading the NIB.

Is there a way to load NIB's depending on the firmware version ? Let's say I have FirstView.xib and FirstView3X.xib

How can I choose which one will be loaded depending on the firmware ?

Thanks

PS: It's my first question on StackOverflow !

A: 

Congratulations to your first question :-) How do you load the nibs? It’s a good idea to split the interface into several files. I usually use a separate nib per every controller and load them using the initWithNibName:bundle: initializer. Since you supply the nib name here, you could easily customize the loading behaviour.

If you load the nibs using the tab bar controller, you probably have all the tabs in one nib. I think I would break the tabs into separate nibs and load them programmatically:

id tab1 = [[UIViewController alloc] initWithNibName:@"Tab1" bundle:nil];
id tab2 = [[UIViewController alloc] initWithNibName:@"Tab2" bundle:nil];
id tab3 = [[UIViewController alloc] initWithNibName:@"Tab3" bundle:nil];
[tabBarController setViewControllers:[NSArray arrayWithObjects:
    tab1, tab2, tab3, nil]];

And since you would now have the control over the nib names, you could easily detect the firmware version and load the correct nib:

id tab1 = [[UIViewController alloc] initWithNibName:
    [self nibNameForCurrentFirmware] bundle:nil];

As for the firmware version detection itself, I think you could use [UIDevice systemVersion].

zoul
The view is loaded by a TabBar. I got the loading nib method but I don't understand how I can load them firmware-dependent !
Frederic Jacobs
since you have a nib loaded by a tab bar, you can use a placeholder nib in the tab bar, then you load a version specific nib when the placeholder is loaded.
Justin
Fine But the loading part of the Nib isn't the problem. The problem is to define whether or not the device is running 4.0 or greater. Can I just compare the strings ?
Frederic Jacobs
Check [this related question](http://stackoverflow.com/questions/820142).
zoul
A: 

You can pull the current version from the UIDevice object:

NSString *osVersion = [[UIDevice currentDevice]systemVersion];
NSArray *ver = [osVersion componentsSeparatedByString:@"."];

If you're happy with comparing strings for single values (e.g. [[ver objectAtIndex:0] isEqualToString:@"3"]). You might want to convert the version numbers to integers so you can compare them for range (e.g., osMajor >= 4).

You can also check for certain classes to exist and load the appropriate NIB based on that:

if (NSClassFromString(@"MKMapView") != nil) {
    /* Do map stuff... */
}
John Franklin
Testing for version numbers is fragile, and he should only use the second half of your answer. Ad the iAds banner programmatically, if the class exists, weak link to the framework, be happy.
jer
What makes testing for version numbers fragile?
John Franklin