简体   繁体   中英

How to use iPhone OS 3.2 functionality in an iPhone OS 3.0 app?

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?

There is a section in the iPad Programming Guide that talks about this titled "Adding Runtime Checks for Newer Symbols". Basically, you can use NSClassFromString and instancesRespondToSelector: to determine if the functionality is there, and then the app can act accordingly.

The two must common ways to detect features are respondsToSelector and NSClassFromString . 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:

if ( [myView respondsToSelector:@selector(addGestureRecognizer:)] ) {
  // assume gesture recognizers exist, create and add some
}

or

if ( NSClassFromString( @"UIGestureRecognizer" ) ) {
  // assume gesture recognizers exist, create and use some
}

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_IDIOM in UIDevice.h where it calls respondsToSelector for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM