开发者

How can i join an iPad and iPhone app?

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 hav开发者_运维知识库e 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.


What you are looking for is this:

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    // Do iPad specific stuff.
} else {
    // Do iPhone/iPod touch stuff here.
}


In XCode 4 at least, you can set which code you want each target to compile, and ignore the rest entirely. So in theory, you could copy all of your iPad app into the iPhone app and then make sure that each only references its own resources and code. The only issue you might run into is naming conflicts, but you can get around this by using groups and folders.

That seems like the simplest way, since you already have both built. The disadvantage with this method is that you will still have to fix bugs in the code separately, but your users will be able to use the same app on their iPhone and iPad.

EDIT:

See BUILD_PHASES > COPY_BUNDLE_RESOURCES & BUILD_PHASES > COMPILE SOURCES


Can't you just join the files together in one app? Like you copy all the source files from the ipad app to the iphones app project (you may have to rename sonme, so there are no duplicate ones)... then in your app delegate you have one if else switch determining whether on ipad and or iphone which adds the required files

This is especially suitable when using an UITabbarcontroller, as you can simply set up the controller with either the rootviews for ipad or for iphone...

All the other source files remain the same (except for renaming mentioned above)... Will increase the file size very much though, i guess..

SideSwipe


you could probably try turning it into a universal app in xCode 4. Here is a link to a short intro that got me on my way to building a universal app.

Universal Apps


Its very simple.

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

    // Do iPad stuff

    } else {

    // Do iPhone stuff

    }

But be carefull while loading the nib names inside the conditions. Hope you know that you have to design the .xib for iPad and iPhone separately.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜