tags:

views:

6169

answers:

8

I've got some code I want to only execute on the latest iPhone SDK (3.0), however I can't seem to figure out a way to target just 3.0 and ignore 2.2.1 etc. There is an ifdef statement, but it just seems to cover the entire iPhone:

#if TARGET_OS_IPHONE

Any help is appreciated.

A: 

It's in Xcode's build options. The drop down on the top left corner that says something like "myapp - 3.0 | Debug" Select the build you want and presto, your done.

Robert Gould
The problem with that method is that I want my app to work on 2.2.1 aswell, with only this one line of code running on 3.0. Your method would mean I'd have to drop support for 2.2x.
Oliver
I see what you mean now, tricky, but you might be able to make a pinpoint check then. Say check for a define in a header.
Robert Gould
A: 

Using the trick of adding OTHER_CFLAGS = "-g3 -save-temps -dD" from this answer, I found that the define __IPHONE_OS_VERSION_MIN_REQUIRED 20000 with a project targeted at 2.2.1. Perhaps that will work?

Jeff Youel
A: 

I'm fairly certain that the comment on the OP sums it up. You can't. Because a compile time directive cannot make a run-time decision. In Xcode, if you target 3.0, it won't run on a 2.2.1 phone. If you target 2.2.1, it won't compile because you have 3.0 specific code in there.

The only true solution would be two versions of your app, one of which is compiled for 3.0, and the other for 2.2.1.

mmc
1st: the OP is actually not excluding compile-time-checks. 2nd: There are exceptions to your given rule. As an example, the MPMoviePlayerController exists on 3.0 and on 3.2 but works differently on 3.2 (and 4.0) - to properly support it on both platforms, I found using a runtime-os-version-check very handy. In my special case, omitting this check will still lead to a proper build on both platforms, but will crash on anything below 3.2.
Till
A: 

My thought would be to test to see if a function exists that only exists on iPhone OS 3.0.

sharth
+33  A: 

You can use this #define to change what you build for each SDK...

#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_2_2
// iPhone 3.0 code here
#endif

And do this at run-time to run code for 3.0 and above:

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 3.0)
    {
    // iPhone 3.0 code here
    }
Jane Sales
I didn't know. Thanks!
Kriem
Great, thankyou!
Oliver
One thing to be aware of is that compiling for the simulator vs. the device can cause surprising changes in these macros. When compiling for the simulator SDK 2.2.1 I get __IPHONE_OS_VERSION_MIN_REQUIRED = 20000. When compiling for the device, however, I get __IPHONE_OS_VERSION_MIN_REQUIRED = 20201, which breaks the condition you've defined above. I'm currently using plain "#ifdef __IPHONE_3_0" to isolate code that only works in 3.0 or higher. That works because the macro isn't defined until the 3.0 SDK.
n8gray
http://cocoabugs.blogspot.com/2010/09/utility-method-to-know-ios4-installed.html
jeeva
A: 

Well, my two cents:

How about building your app with the latest version say 3.0 so you can exploit all the new & cool APIs and specifying the Deployment Target as the least recent version you want to support so guys out there who didn't take time out to upgrade their devices will stil run your app. In this case as shown above you need to check for least recent version of the SDK and provide alternate functionality to make your application backward compatible.

Regards, Hardik

A: 

Im getting some odd behaviour after setting base sdk = iPhone device 2.2.1 and deployment target iphone os 3.1.2

This code OS_VERSION = [[[UIDevice currentDevice] systemVersion] floatValue]; NSLog(@"running version:%f", OS_VERSION); NSLog(@"minimum version:%f", __IPHONE_OS_VERSION_MIN_REQUIRED ); NSLog(@"2_2 version:%f", __IPHONE_2_2 );

prints

running version:3.100000 minimum version:3.099998 2_2 version:3.099998

on the simulator and

running version:3.100000 minimum version:0.000000 2_2 version:0.000000

on the device

and I dont have an iphone_os_2_2_1 define

suggestions welcomed.

Martin West
Can you please add better formatting to your comment?
Heath Borders
-1: Please don't post questions as answers. Add a comment, or start a new question http://stackoverflow.com/faq.
Casebash
A: 

Pretty sure you can also do:

#ifdef __IPHONE_3_0
// 3.0 code here
#endif
BadPirate