Universal iPhone/iPad application - iphone

I know, how to detect whether it iPhone or iPad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
I know 2 ways to build universal app:
1) use one controller for 2 xibs (iPhone/iPad)
2) use different viewcontrollers for iPad and iPhone
What's the best way to do that?

Same View Controllers for iPhone & iPad, but using UIUserInterfaceIdiom as switch case to avoid duplicated view controllers ( with same contents ) created.
You can use different XIBs for iPhone & iPad though.

I think use different XIBs is better because use different view controllers means you have to main two almost same code. And on the XIB you can clearly see what you will get using that XIB for iPhone or iPad.

Related

Upgrade iPhone-Only app to Universal doesn't create -ipad classes

I'd like to upgrade my iPhone-only app to Universal. So, from the targets' general window I switch from iPhone to Universal and select "copy" for generate iPad Storyboard based on the iphone one. Now a new folder named iPad is created but is empty and I can't find all of the -ipad classes. What I want is to have all of the original (iPhone) classes duplicated with the -ipad suffix and a iPad dedicated Storyboad. Starting from these classes, and from the iPhone logic I want create step by step a new ipad version. In the targets' general windows ipad main interface is main_iphone-ipad but in the project doesn't exist, in the folder neither.
This isn't an issue with Xcode, thats what its supposed to do.
You can certainly duplicate your entire project for the iPad version manually if you'd like, but I can't think of a case where you'd actually want to do this.
Most of the time you just want to change the View layer between the iPad and iPhone version. You can load a different Nib based on the device you'd like or even load completely different view controllers doing simple logic like
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
viewController = [[MyViewController alloc] initWithNibName:#"ipadNIB" bundle:nil];
} else {
viewController = [[MyViewController alloc] initWithNibName:#"iphoneNIB" bundle:nil];
}
** BEGIN EDIT **
To setup a different storyboard for iPhone/iPad all you need to do is create another storyboard and name it "myprojectname_ipad.storyboard" then in the Info.plist for your project add a key called "Main storyboard file base name (iPad)" with a value "myprojectname_ipad"
** END EDIT **

Coverting an iPhone targeted app to Universal in Xcode

I have a project that was intended to be an iPhone app in first instance, but now I want it to also support iPad. I have changed project's target from iPhone to Universal, but I dont know how to manage both nib versions I need now, already having nibs designed for iPhone. I tried by loading same viewControllers with different nibs according to the device, but Im not allowed to set more than one interface control to the same outlets. Any help? I have Xcode 4.4
It's pretty easy.
For your storyboard files, just include the _iPad and _iPhone suffixes.
For example, if the original storyboard name is MyStoryboard.storyboard, you'll now have MyStoryboard_iPad.storyboard and MyStoryboard_iPhone.storyboard. You can also set the storyboard file for each device under the summary view of the project.
Then, for the.xib files, include the ~iPhone and ~iPad suffixes.
ViewController.xib becomes ViewController~iPad.xib and ViewController~iPhone.xib
To start off, you can just create a duplicate if your existing .xib files and rename them to have the iPad and iPhone suffixes. Then alter the contents of each .xib as needed.
As for the .m code, you can check device type and branch your code. My approach is to define macros that identify device type, like this:
#define isDeviceIPad (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
#define isDeviceIPhone (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
Then I just use these macros(which return bool values) to write code that's specific to a device type(such as placement or dimensions of a particular view etc.).
You can change only targeted device as iPhone / iPad and you can use the .xib file of iPad:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
//For navigate to iphone view
}
else{
//For navigate to ipad view
}
Reference
Add 1 more XiB targeted for iPad to each file and during Navigation, select the Xib after checking Device type with the code shown above

How To Port an iPad Application to the iPhone

I have an existing iPad - Application, no I want to make an universal App.
There are many Tutorials with making iPhone Apps universall, but i dont find how to make iPad apps Universal. Is there somebody having experienxce with this?
No Storyboards are used, yust normal xib.
I've readed this tutorial, and porting iPhone apps to universal seems to be better supported by xcode:
http://www.raywenderlich.com/1111/how-to-port-an-iphone-application-to-the-ipad
I would think that the basic idea would be the same, whether you were converting an iPhone app to a universal app, or an iPad app to a universal one(?)
Set the iOS application target to "Universal".
If a view controller, say, XYZViewController has an associated xib for its UI, then - when you create a xib for the iPhone interface - append ~iphone to the name of the iPhone interface and ~ipad to the iPad one, i.e. their file names are
XYZViewController~iphone.xib and XYZViewController~ipad.xib. That way the right xib will be picked up if you pass nil or #"XYZViewController" as nib name to the designated initializer for the view controller. (It's worth noting that the same IBOutlet can be hooked up to the corresponding UI elements in both xibs simultaneously.)
In code, whenever you need to use a different display metaphor that depends on the device type (say you want to use , check for the device type through the test
if ([UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{ ... } else { ... }
Another place where you might want to use this test is in VC's shouldautorotatetointerfaceorientation: method.

Converting an iPhone app to a Universal app?

I have an iPhone app with
FirstViewController and SecondViewController with respective views FirstView.xib and SecondView.xib.
Now I want to make this app work with both iPhone and iPad. For iPad I need to merge Firstview.xib and SecondView.xib into a single ThirdView-iPad.xib.
What would be the best approach? Do I need to write another ViewController class for iPad or can I use existing FirstViewController and SecondViewController with single xib?
My research so far says that there is no way to use multiple ViewControllers with single xib. Please let me know the best way to do this.
Thanks
if you are using Xcode4 you can use "transition to universal target" and it will do everything needed
http://xcodebook.com/2011/05/making-an-ios-target-universal/
if you dont have it then I recommend to keep controllers out of your xib. xib files will be only containing views for iphone and ipad but the controllers "can" be same. and you can control your logic from the rootviewcontroller, just an idea..it all depends on your project.
You would be best of to just create a new ViewController unless you're into masochism. Reusing in that way would most likely be to much trouble, and I'd go with aggregation or inheritance in the case you need to reuse logic in your ViewControllers.
I typically create a base ViewController, say XYZViewController and then create a XYZViewController_iPhone with all iPhone-specials and a separate XYZViewController_iPad for iPads. But if they're totally different I give them unique names and their own NIBs as well as ViewControllers.
There Are following Rules To Convert The Iphone App to Univarsal App
1) In Plist File Add NSMainNibFile~ipad .... MainWindow_ipad(ipad window).
2) Implement separate Names Xibs (Iphone & ipad)
3)in Target Targeted Device Family set to (iphone/ipad)
and set The Frames According To Ipad &iphone
For example
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
btnHme.frame=CGRectMake(971,6, 32, 32); //ipad
}
else
{
btnHme.frame=CGRectMake(438,6, 32, 32); //iphone
}
cheers

can TTURLMap from three20 map to a xib-file?

ive made an app for the iphone and wanted to make it work on an ipad too.
for the navigation i used three20. all the views are viewcontrollers with xib´s.
for later developement, i just wanted to make the xibs in the bigger format as new skin and let the background-programm the same, so that in the end each viewcontroller would have 2 xib´s as skins.
so, three20´s URLMap can link to class-files. but how can i link to a xib ?
i wanted in the appdelegate to make the URLMap depending on the device, so that the programm it self can stay as is with its URL-Calls.
ok, now i used to overwrite the initWithNibName in combination with [UIDevice currentDevice].model to let the viewcontroller check, on which device he loads