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?

BeachRunnerFred picture BeachRunnerFred · Jul 23, 2010 · Viewed 10.3k times · Source

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 two different versions of the iOS since iOS 4 isn't available for the iPad yet. There are two iOS 4 features (GameCenter and iAd) that I would like to support in my game.

  1. In this case, is it a bad idea to create this project as a universal app?
  2. If not, what are some thoughts I should take into consideration as I'm building a universal app that supports two different versions of the iOS?
  3. Is it somewhat safe to bet on Apple releasing the iOS 4 for the iPad in the fall as speculated?
  4. If so, is there anyway I can begin building these iOS 4 features (GameCenter and iAd) into my iPad version of my game?

Thanks so much in advance for all your wisdom!

EDIT: I understand this issue involves managing risks. I'm aware of the risks, but I'm more interested in any tech design considerations related to building a universal app when the iOS is fragmented among the various iOS devices.

Answer

Benjamin picture Benjamin · Jul 23, 2010

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.