views:

1011

answers:

6

I've been working on iPhone app for awhile and still want to support 2.2.1

One of the features is that the user can start the app via email by clicking a link. In the 2.2.1 world, I accomplished this by implemting the application: handleOpenURL: message.

In iPhone 3.0, they've changed things up adding the application: didFinishLaunchingWithOptions: method (which is great, and makes a lot more sense than the old way). In that method, you use the key UIApplicationLaunchOptionsURLKey to find out what the URL was.

The problem is, if I use that key my app doesn't build in 2.2.1 since it was introduced in 3.0. What's the most elegant way to get around this and still support 2.2.1? I was thinking of using the actual value for the UIApplicationLaunchOptionsURLKey enum, but I figured that was ugly. Has anybody encountered this and think of a better way?

+2  A: 

The simplest way is to #define around the 3.0 code and 2.2.1 code so you can do conditional compilation. Note: This means you will have one binary for 3.0 and another for the other.

so

#ifdef IPHONE_OS_3.0

/* DO 3.0 stuff */

#endif

#ifdef IPHONE_OS_2.2.1

/*DO 2.2.1 stuff */
#endif

In response to your comment, you would have to have a different #ifdef #endif for each code block if the code is going to be different, otherwise if it is only different for 3.0 you would do something like

#ifdef IPHONE_OS_3.0

/* DO 3.0 STUFF */

#else
/* DO STUFF FOR OTHER THAN 3.0 */
#endif

You are going to have to figure out what the real definitions are (I just made them up :))

Hope that helps

hhafez
If I do this, do I have to do an #ifdef for not only IPHONE_OS_2.2.1 but every other version before it?
bpapa
see my response to your comment :)
hhafez
If the aim is to have a single binary that supports different functionality depending on the OS, this is the wrong answer, as it's a compile-time check.
grahamparks
Agree with that
hhafez
A: 

There's a question about 3.0 features in 2.0 iPhone OS. But you want to know (at run time) if a symbol exits in the UIKit or whereever. I'm afraid, that's not answered there, yet.

Nikolai Ruhe
A: 
#ifdef __IPHONE_3_0
// iPhone 3.0 specific stuff
#else
// iPhone 2.2 (or below) specific stuff
#endif
micmoo
A: 

To have a single app that works on both OS versions, you have to build against the 2.2.1 SDK. And since that SDK has no idea the UIApplicationLaunchOptionsURLKey symbol, you have no choice but to define it in your code if you recognise it value when your app is run on OS 3.0.

On the other hand, adoption of OS 3.0 is very strong (upwards of 75% already, according to one figure I've seen), so making your app OS 3.0 only is also a solution to consider.

grahamparks
this isn't true. You can build against the 3.0 SDK but set the required OS version as 2.2.1 (or below)
David Maymudes
+1  A: 

Use the 3.0 SDK, but set the "iPhone OS Deployment Target" to 2.2.1 or earlier.

Under OS 3.0, your application: didFinishLaunchingWithOptions: will get called, and if you're running on OS 2.2.1, one of the old methods will get called.

If you're not trying to use any other 3.0 features, I don't think you'll need to do anything else, but you could also look at my other answer about Apple's MailComposer sample.

David Maymudes
A: 

So what should you too if you want to have a single binary that supports multiple OS versions? I'm especially stuck on how to conditionally load the necessary libraries for 3.0.