views:

254

answers:

2

I have an application designed for iPhone OS 2.0 and I am adding some new 3.x functionality to it. My idea is to maintain compatibility with older versions.

I have managed so far, to test for deprecated functions using "if respondToSelector...". This is just fine for calls inside a method but how to deal with method name changes? For example, the OS 2.x method

-imagePickerController:didFinishPickingImage:editingInfo:

changed in OS 3.x to

-imagePickerController:didFinishPickingMediaWithInfo:

How can I test for the OS version and direct the application to the proper method in this case?

thanks for any help.

A: 

OK.

After a second read, I found, that I did not get your problem completely. If you implement both methods, either one will be performed at runtime. If it is a 2.x the -imagePickerController:didFinishPickingImage:editingInfo: will be performed, if it is 3.x the other one will be called

This is the crap I have written prematurely and where the comments are related to:

you may use

#ifdef __IPHONE_3_0
// iPhone 3.0 specific stuff
#else
// iPhone 2.2 specific stuff
#endif

see also this post:

http://stackoverflow.com/questions/146986/what-defines-are-setup-by-xcode-when-compiling-for-iphone

maxbareis
This only works at compile-time and will not do what he wants. If he's compiling for 3.x, but targeting 2.x, it will build the 3.x code which will fail on a 2.x device.
Brad Larson
You are right, I was a little bit too fast for that.
maxbareis
+3  A: 

You can use NSObject's -respondsToSelector method to dynamically determine if the method exists, then call it. You might also want to use -performSelector:withObject: to call the methods, so you don't get compiler warnings.

chpwn
If you read what I said you will see that this method I am talking about is called automatically by the delegate (or in the delegate).
Digital Robot
Oh, for that just implement both and have them just call a third, internal method. The unused one is not harmful.
chpwn
In the case of a deprecated method, *both* the old/new variants are available. So let's my base SDK is 4.0, and the deployment target is 3.0 (which a significant number of my customers will have for the interim, if the 2.0-to-3.0 upgrade period was any indication - free upgrades this go-round notwithstanding), using -respondsToSelector: won't help here, nor will #if/#else tests ... will they? Or do I have to get the iOS version at runtime? (Shudder.)
Joe D'Andrea
Yeah, I'd grab it from UIDevice.
chpwn