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 957
孤城傲影
孤城傲影 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 06:59

    If you're about to build a Universal App, just remember the following two source code snippets:

    • Using classes only if they are available on the current device

      Consider this piece of code:

      UILocalNotification* n = [[UILocalNotification alloc] init];
      

      When building a Universal App, this code will lead to a runtime error on any device running an iOS version that does not know about the UILocalNotification class.

      You can still support UILocalNotification in your code while maintaining backwards compatibility by using the following code snippet:

      Class notificationClass = NSClassFromString(@"UILocalNotification");
      if (notificationClass) {
        UILocalNotification* n = [[notificationClass alloc] init];
      }
      

      This technique allows you to use classes which are unavailable in certain OS versions on devices that do support the classes.

    • Using methods only if they are available on the current device

      Suppose you'd like to do the following:

      [[UIApplication sharedApplication] scheduleLocalNotification:n];
      

      Use the following piece of code to conditionally call the method in case it's available on the current device:

      if ([UIApplication instancesRespondToSelector:@selector(scheduleLocalNotification:)]) {
        [[UIApplication sharedApplication] scheduleLocalNotification:n];
      }
      

    These two techniques are probably the only things you need to know to create your Universal App. Conditionally execute your code depending on device capabilities and you should be fine.

    Having said that, you still need to consider that the iPad will probably be using a different UI than your iPhone counterpart. And, unfortunately, you won't be able to test your iPad UI with the iOS 4 features until they become available on the iPad. But this shouldn't be too much of a problem: if you use [[UIDevice currentDevice] userInterfaceIdiom] to check whether you're running on an iPad, you can prevent your Universal App from executing code that has no iPad UI yet. Once Apple releases iOS 4 for iPads, you can implement the UI, remove that check and release an update to the store.

提交回复
热议问题