What do I need to do to adapt my application to iOS 5.0, while maintaining compatibility with iOS 4

I started playing with iOS 5 today, and I saw that Xcode 4.2 allows me to select only iOS 5 as the base SDK, but not iOS 4.

So far, I am rewriting the drawRect: method in the UINavigationBar to customize its appearance, but iOS 5 no longer calls this method. Now I have to use [UINavigationBar appearance] to do this (which, I think, is much better). However, the appearance method is only available in iOS 5, so if I use it in my application, it crashes when executed on iOS 4. What should I do? Do I need to check the iOS version with macros in every place where I use the iOS 5 method?

Thank,

Ariel

+5
source share
5 answers

The answer to your first question: you must use iOS5 (or the latest version of the iOS SDK) as the base SDK, but you will install the minimum version of iOS running Deployment Target. There you can install iOS4.0 (or whatever).

The correct way to solve the second question is to test the capabilities, not the version. So something like this will work, say, in your method application:didFinishLaunchingWithOptions::

// iOS5-only to customize the nav bar appearance
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
    UIImage *img = [UIImage imageNamed: @"NavBarBackground.png"];
    [[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}

You will then compile this against the iOS5 SDK, so using it appearancein general will be great. But when this compiled code runs on iOS version 5, it will be fine.

As already mentioned, you can save the code drawRect:as is.

+16
source

.

UIImage *image = [UIImage imageNamed:@"header.png"];

if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    //iOS 5 new UINavigationBar custom background
    [navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
} 
else{
    UIImageView *imgView = [[[UIImageView alloc] initWithImage:image] autorelease];
    [imgView setUserInteractionEnabled:NO];
    [imgView setTag:TOOLBAR_TAG];
    [navigationBar insertSubview:imgView atIndex:0];
}

responsesToSelector, , .

+3

drawRect: iOS 4, -, [ UINavigationbar]. .

0

, , .

Soooo... - :

NSString *os_version = [[UIDevice currentDevice] systemVersion]; 

iOS, , [UINavigationBar] 5 , drawRect iOS 4.

0

Also take advantage of the download of 4.3 simulators for iPhone and iPad. Then you can crash faster when you accidentally use iOS 5 on 4.3 --Tom

0
source

All Articles