开发者

Enabling different features depending on iOS version

开发者 https://www.devze.com 2023-01-28 05:26 出处:网络
In the application we\'re building, we want to use UILocalNotifications, which are only available from 4.0.

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:开发者_如何学JAVA(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.


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!


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.


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

Supporting older versions of iOS while using new APIs


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消