简体   繁体   中英

What should I do to adapt my app to iOS 5.0 keeping compatibility with iOS 4

I've started playing with iOS 5 today and I've seen that XCode 4.2 only let me select iOS 5 as Base SDK but not iOS 4.

Until now I've overwriting drawRect: method in UINavigationBar to customize its appearance but iOS 5 doesn't call that method anymore. Now I've to use [UINavigationBar appearance] to do it (which I think is much better). However, appearance method is only available in iOS 5 so if I use it my app it crashes when executing on iOS 4. What should I do? Do I have to check iOS version with macros in every place I use a iOS 5 method?

Thank you,

Ariel

The answer to your first question is: You must use iOS5 (or Latest iOS SDK) as your base SDK, but you set your minimum supported iOS version under Deployment Target. There you can set iOS4.0 (or whatever you want).

The correct way to deal with your second question is to test for capability , not version. So, something like this would work in say, your application:didFinishLaunchingWithOptions: method:

// 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 be compiling this against the iOS5 SDK, so the use of appearance at all will be fine. But when this compiled code runs on a version of iOS before 5, it will be fine.

As said before, you can keep your drawRect: code as-is.

Another way to customized your header is like this.

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];
}

Using the respondsToSelector you can know if the function is here.

You can put the same piece of code in both the drawRect: that iOS 4 uses and the proxy returned by [UINavigationbar appearance]. Two different code paths.

You can't do this with macros since both code paths have to be in place and the correct route to go depends on a run-time check.

Soooo... use something like this:

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

to get the iOS version you're currently running on and do the [UINavigationBar appearance] under 5 & newer, and you can fall back to the drawRect thing on iOS 4.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM