I have an iPad app and an iPhone app.
I would like to join them so that if the join app will run on the iPad it would have a behaviour and if it runs on the iPhone it will have another.
The first thing that i thought was to make different views and select the view for the iphone/ipad but i also have some things specific to the ipad and iphone (example : splitViewController) so only selecting a different view won’t help.
What is the best way to join 2 such apps ? i would like a more elegant method than doing something like
if(isIPhone)
{
// code for iphone
}
else if(isIPad)
{
// code for iPad
}
every time i have a difference between the 2 versions. Thanks!
I believe that the WWDC 2010 session videos include an iPad Development Overview that covers exactly this issue.
Here’s the approach I’ve taken, according to Apple’s guidance:
You can set platform-specific Main Interface nibs, which means you can set platform-specific app delegates as well. So you perhaps have a superclass app delegate that does all your app setup, then subclasses to deploy either an iPad UI or an iPhone UI.
This is helpful since, on the iPad, split views need a traffic cop to determine what content goes where. Now you’ve got a spot to put that logic independent of the view controllers: in the app delegate.
From there, any time there is a view controller that is common to both platforms but has some specific behavior for each, a view controller superclass is created that handles all common functionality. An iPad-specific subclass might then override the -refreshView: method to do some additional animation, or to add properties and delegate methods to support split views.
While you can definitely test for iPad-ness with
if else, this can get really gnarly in a view controller as you add delegate methods that only matter on one platform. By splitting it up this way, you get code that’s a lot easier to maintain and read. This can all mean a lot of refactoring of an existing project. In my case, though, it only took about an hour and the result is a very tidy app that’s easy to jump in and modify for one or both platforms.