Problem: My app must run on iPhone OS 3.0.
However, there are some features of iPhone OS 3.2 which I really want to use. Just as a little add-on for free. But I don’t want to cut off my user base by doing this.
Imagine you’re an iPhone OS 3.0 thing, and someone gives you a book to read. It has iPhone OS 3.2 instructions. You never learned those. So what do you do? Crash? They would have to be hidden, so you’re not bothered.
Someone wrote recently on SO:
keep in mind that you have to check
the version at places in the source
code where you like to use the new
features and provide alternatives for
older os versions
So how could I do that? Wouldn’t Xcode throw warnings when it finds stuff that isn’t linkable from anything anywhere? Would I just check for the OS version and dynamically link – somehow – to whatever stuff I think is cool?
The two must common ways to detect features are
respondsToSelectorandNSClassFromString. With these you can tell if an old class has a new method, or if a new class exists.For example, 3.2 added gesture recognizers. You could use either one of these methods to decide if you want to add gesture recognizers to a view:
or
In both cases, you would build for 3.2 but only use 3.2 features if they are detected at runtime. If you build against 3.0 and all the warnings show up in places where you are checking things correctly, then you are good to go.
In the case of 3.2 a new processor was also added, so either build a universal binary or build for the older architecture.
Also, in some cases Apple provides support for detecting version. A good example of that is
UI_USER_INTERFACE_IDIOMinUIDevice.hwhere it callsrespondsToSelectorfor you.