Objective-c – Using iPhone OS 3.0 features if available, and 2.1 features if not, in one executable

cocoa-touchiphoneobjective c

I've seen apps on the iPhone that if running on 3.0 will use 3.0 features/APIs such as the in-app email composer, and if running on 2.x not using those features, and exiting the app to launch Mail instead.

How is this done?

My initial thoughts were to use

#ifdef __IPHONE_3_0

but that will only work if I actually build the app against the 3.0 SDK – which then prevents it from running on 2.x phones.
Also, running with that thought, the app has to be linked against the 3.0 SDK anyway to get the 3.0 APIs…

I'm a little confused as to how it's been achieved.

Best Answer

You set the Base SDK to 3.0, and the Deployment Target to 2.2.

You then wrap all the 3.0-specific (or higher) methods in NSClassFromString and respondsToSelector statements.

It will work on the device in 2.2, but not in the simulator.

The advantage of this method is that you don't tie your code to specific version numbers - just whether the methods or classes exist or not.

This page explains it pretty well.

http://www.clarkcox.com/blog/2009/06/23/sdks-and-deployment-targets/