sharvey
sharvey

Reputation: 8185

Enabling different features depending on iOS version

In the application we're building, we want to use UILocalNotifications, which are only available from 4.0.

Checking if an object responds to a selector is pretty easy, but how can I go about defining delegate methods on a class depending on the OS? The problem I get is that my app delegate implements the

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

method, which makes the application crash on startup on a iOS 3.2 device (ipad).

Not having to release multiple builds would be preferred.

Upvotes: 3

Views: 2199

Answers (4)

Matthew Frederick
Matthew Frederick

Reputation: 22305

Marco Arment has a recent post about it, with good insights:

Supporting older versions of iOS while using new APIs

Upvotes: 0

Count Chocula
Count Chocula

Reputation: 1041

You need to use a feature called “weak linking,” which will work with iOS 3.1 or later. There are some very specific steps you need to follow, which are outlined in the 4.2 “what's new” document (search for “weak linking” in the document).

HTH!

Upvotes: 6

Tom Harrington
Tom Harrington

Reputation: 70976

You could change the delegate methods to use id as the type for classes not available on 3.2, that is:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(id)notification 

In the method body, check for UILocalNotification as usual. The notification argument can be typecast to UILocalNotification.

I'm surprised this causes a crash though, since the method shouldn't be getting called. If changing the type in the method definition doesn't help, it may be that the method is getting called for some reason. In that case try setting a breakpoint in the method to find out why.

Upvotes: -1

Sam Ritchie
Sam Ritchie

Reputation: 11038

One nice way is to check this:

#ifdef __IPHONE_4_0 // if iPhoneOS is 4.0 or greater then __IPHONE_4_0 will be defined

    //code
#endif

If you're dealing with an OS below 4.0, if won't even see any code within that compiler directive.

Upvotes: 3

Related Questions