Universal Apps concept for iPhone and iPad - iphone

am a noob at universal apps so please help me out and let me know if this can be done, I am creating a universal app and for doing this i have written the code and made the settings in each of my view controllers present in the iPhone and iPad group and at runtime i am identifying the device whether its iphone or ipad i am using the below method
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
myiPad_View *ipadObj = [[myiPad_View alloc]init];
[self.window addSubview:ipadObj.view];
}
else
{
myiPhoneView *obj = [[myiPhoneView alloc]init];
[self.window addSubview:obj.view];
}
Now the query is my boss is eating my head for this he is telling me that can we write just one class and at run time when the app will be launched either in iphone or ipad device or simulator the view controller will adjust the frames accordingly i have done this by using the above method but he says if you do like this then the code will increase and you have to suffer a huge headache so actually he is right but haven't found of any alternate solution to this, so before saying this cant be done i thought of taking some expert advice

Your above method is actually how I determine devices in some of my apps as well.
I actually recently wrote a little piece on this issue.
You can also use:
#define IDIOM UI_USER_INTERFACE_IDIOM()
#define IPAD UIUserInterfaceIdiomPad
#define IPHONE UIUserInterfaceIdiomPhone
if ( IDIOM == IPAD )
NSLog(#"Device is iPad.");
else
NSLog(#"Device is iPhone or iPod touch.");
or
if ( [[[UIDevice currentDevice] model] isEqualToString:#"iPod touch"] )
{
NSLog(#"Device is an iPod touch");
/* same goes for "iPhone", "iPad" etc. */
}

I have 3 sets of classes:
base classes: this is where the shared logic goes
iPhone classes: iPhone specific logic including UI goes here
iPad classes: iPad specific logic, including UI goes here
I have as much as possible in the base classes, but for the times when the logic is separate, it is much easier to have the distinct classes. Digging through the code looking for the exceptions would be a pain, IMO.
I found this blog helpful (not mine)

Its not a problem . you have to create a universal app.
File--->New Project--->windowsapplication
and then choose a product Universal.

Related

Is it possible to create application with half part compatible for iphone only and other part is for universal?

I am developing an application, in it as the client requirements, few views are compatible for only iPhone 4 devices and other views are compatible for both like iPhone 4,5 /iPad. So is it possible in single build?
I know it is not possible but I want your suggestion for the same.
If you have any link related to that then please share with me.
Yes, it is possible to have different views for iPhone 3.5", iPhone 4" and iPad. Also, you can implement different features in every different device. However, iOS User Interface Guidelines recommends that you provide same functionality across different devices.
Preserve the primary functionality of your app, regardless of the device it runs on. Even though one version might offer a more in-depth or interactive presentation of the task than the other, it’s important to avoid making users feel that they’re choosing between two entirely different apps.
In practice, you can programmatically write a bifurcation to build (programmatically) a view using this macros:
#define isIpad() \
([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)] && \
[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad)
#define isWidescreen ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 )<DBL_EPSILON )
#define isIphone ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: #"iPhone" ] )
#define isIpod ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: #"iPod touch" ] )
#define isIphone5 ( isIphone && isWidescreen )
Also, you can create different xib files for every different devices, using the bifurcation and UIViewController message: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil, to load the correct file.
Finally, you can use modifiers in the xib filenames to load different resources between iphone and ipad. For example: You have FirstViewController.xib for your iPad UI; you can create a FirstViewController~iphone.xib that will be loaded automatically by iOS if the device is an iPhone (instead of FirstViewController.xib). You can check this answer for more information about this subject.
EDIT
In the case that you have already "iPhone only xib files" and you want to run those on iPad, you should consider to copy each of those files, and convert the copies to iPad target as this or this answers explains . Then, you can re-name this copies ending with "~ipad.xib", that should solve your problem, however, this views will need a layout revision.
just decide this by code. if a view shall be available for iPhone, call it, and if not just let it be. to distinguish between iPhone and iPad just use
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// iPad
else if([[UIScreen mainScreen] bounds].size.height == 568)
{
// iPhone retina 4 (iPhone 5)
}else
// Older iPhones

Checking if user's device is ipad or iphone/ipod, difference between two ways

I'm reading Apress - Beginning iPad Development for iPhone Developer Mastering the iPad SDK and in one chapter i read about device checking and author write:
"Although you may be temped to simply check the user's device type or operating system version, with Apple constantly releasing new devices and iOS versions, that's not the way to go. A better approach is to test for the availability of exclusive iPad classes using NSClassFromString. If you pass an iPad-only class name, such as UISplitViewController to NSClassString and a valid object is returnet, you'll know user's device is an iPad.."
I don't understand, why simply check device type for example by:
NSString *device = [[UIDevice currentDevice] model];
if ([device rangeOfString:#"iPad"].location != NSNotFound ) {
isIPad = true;
}
else isIPad = false;
Is worse than checking ipad classes?
The way that Apple checks in their own examples and when you start a new universal application is like so:
-(BOOL) isiPad {
return UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad;
}
And the reason why using model is probably bad is because you're just using a string comparison. While I doubt they will change it, you never know when they might decide to change the string returned by using [[UIDevice currentDevice] model] to model number or something else.
may be this method help you
-(int)CheckDevice {
return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) ? 1 :([[UIScreen mainScreen] bounds].size.height == 568) ? 5 :4;
}
it return
1 for ipad
4 for iphone 4
5 for iphone 5

Converting IPad App to Universal App

We currently have an app that targets the IPad. We now would like to target the IPhone aswell. Im just wondering how we go about this? UI layout and functionality will be different but under the hood they will be sharing a lot of the same code.
Am I best creating a Universal app? or having to seperate apps one for the ipad and one for the iphone?
Any help will be greatly appreciated.
Thanks
Use these two macros
#define FOR_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define FOR_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
if(FOR_IPAD)
//load view for iPAD
else
//load view for iPhone

Making 2 Separate App Delegates For Universal App?

Should I make 2 separate app delegates for a universal app? One myAppDelegate, then myAppDelegate_iPhone and myAppDelegate_iPad which are subclasses? Reason being iPhone should have IBOutlet of NavController while iPad should be UISplitViewController.
Also, how do I separate actions between iPhone and iPad. For example, a button on iphone may push a view, but on iPad i want to have a small window popup instead rather than a full-screen push. Do I just use a if/else statement to check if iPad (by uiswitchviewcontroller), then go from there?
if (NSClassFromString(#"UISplitViewController") != nil && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//currentDeviceType = iPad;
}
else {
//currentDeviceType = iPhone;
}
This question seems to be along similar lines.
Universal iPhone/iPad AppDelegate
This doc from Apple might also prove helpful.
Introduction to Universal Apps
No need to take two separate application delegates. You can code on the condition...
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){}
This will automatically detect the device.
To enable rotation on the device, you need to return YES to
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
FOR ALL OF YOUR VIEW CONTROLLERS.
You should also add your supported orientations to your plist or info under xcode 4.
Also keep in mind that stack overflow prefers that a new question be asked when the question topic changes :)

iphone to ipad xib file change

I have the two xib files 1.main 2.language translator this is a iphone app.
I want to change UNIVERSAL APP. i upgrade the universal application. now automatically created mainwindow-ipad.xib.
i run this app in xcode 3.2 version but it display left corner only .
how can i change this app to universal ... its big headache to me.. anyone help me
Thanks in advance,
Suresh.m
Check whether app is on iPhone or on iPad and accordingly load the nibs, I mean u have to keep the Ipad versions of ur interface builder files and load them according to the target device
if([[UIDevice currentDevice]interfaceIdiom]== UIUserInterfaceIdiomPad)
{
//Load ur ipad nib file
}
else
{
//Load ur iphone nib file
}
I usually create a new universal project, and then "move" my code into the template provided. I find that much easier then trying to flip all the other knobs.
I found iphone_bharat's example interesting, but it has a small type so I had to fix it and clean it up a little bit.
+ (bool)iPad
{
// Can use UI_USER_INTERFACE_IDIOM() for iOS less than 3.2
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad )
return YES;
else
return NO;
}