开发者

Graceful degradation on iPhone

How do I write a program for iPhone (Objective C++) that runs on OS 2.0 but takes advantage of 3.0 features if they're available?

Example: copy&paste (class UIPasteboard).开发者_如何学编程 Nice feature to have, but I don't want to kill backward compatibility. Do I compile against SDK v. 3 or v. 2? If the latter, how do I create an instance of UIPasteboard, considering it's not declared in the headers? If the former, won't some C-linkage functions cause "unresolved reference" upon loading under OS 2.0?


Edit your Target's build settings like this:

  • Set the Base SDK to the version whose APIs you want to use (e.g. 3.0 or 3.1).
  • Set the Deployment Target to the lowest OS version you want to support (e.g. 2.2.1).

When building, you compile against the Base SDK. For all symbols that defined in your Base SDK but not available in your Deployment Target, you need to add runtime checks to your code to check for their availability. Examples:

  1. Check if UIPasteboard is available:

    Class PasteboardClass = NSClassFromString(@"UIPasteboard");
    if (PasteboardClass != nil) {
        // UIPasteboard is available
    }
    
  2. Check if a specific method is available:

    if ([UITableViewCell respondsToSelector:@selector(initWithStyle:reuseIdentifier:)]) {
        // ...
    }
    


I didn't try this but i would recommend building against the most recent (3.x) SDK release. So you get any class and method definitions that might be available on the target device.

And in your application you have to check the OS release your application runs on. Based on the target OS you have to decide which Class and Method you should use. After all it is a big mess of conditional code, probably with a lot of additional code to provide missing functionality (i.e. direct access to SQLite instead of using Core-Data). In my experience that should not lead to problems, because most type information is erased at runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜