How should I approach building a Universal iOS app that will include iOS 4 features, even though the iPad doesn't yet run iOS 4?

后端 未结 4 969
孤城傲影
孤城傲影 2021-01-31 06:19

I\'d like build a game for both the iPhone and iPad. As such, it would make sense to start this project from scratch as a universal app. However, iPhone and iPad currently run

4条回答
  •  天涯浪人
    2021-01-31 07:01

    You mainly need to do two things:

    1. Weak-link any frameworks that aren’t present in the 3.2 SDK.
    2. Write runtime tests for any APIs that are new to iOS 4.

    To do the first, right-click on your target and select “Get Info.” The frameworks are listed in the inspector (under the “General” tab) with a drop-down box next to them that’ll allow you to select “Weak” linking. This ensures that the app will still run if the framework is not present.

    To do the second, you could do something like the following to test for the new block-based animation in iOS 4:

    if ([UIView respondsToSelector:@selector(animateWithDuration:animations:)]) {
        // Your awesome animation code here
    } else {
        // Your almost-as-awesome, non-block-based animation code here.
    }
    

    By using introspective methods such as -respondsToSelector:, you can avoid calling something that the currently-running OS doesn’t support.

    Note also that if you wanted to support iPhone OS 3.0, these same rules would apply.

    Finally, it's also possible—though not advisable—to do this like so:

    #ifdef __IPHONE_4_0
        // Your iOS 4.0-compatible code here
    #elif defined(__IPHONE_3_2)
        // Your iPhone OS 3.2-compatible code here
    #elif defined(__IPHONE_3_0)
        // Your iPhone OS 3.0-compatible code here
    #endif
    

    Why is this not advisable? Simple: only the code for the highest-numbered iOS version will be compiled. iPhone apps aren’t compiled separately for separate iOS versions, so to get this to actually work, you would have to release multiple versions of the app.

提交回复
热议问题