how to do different coding for iphone and ipad in same .m file in universal app? - iphone

I have created app for iPhone. I have no of table views, image views, map views in my app. All data coming through web service. And I've given some static sizes for image and other views through coding. now, I need the same app for iPad. So i have to change coding of view sizes for Ipad. I need to do different coding for iPhone and iPad in the same .m file. Anyone pls suggest me how to do that!!!

Till i understood your question, i think this is what u need:
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
// place the iphone code
}
else
{
// place the ipad code
}
and you can refer to this post : http://iphonedevelopment.blogspot.in/2010/04/converting-iphone-apps-to-universal.html

As you can see when you choose a universal app in the starting when you are taking a new project - in universal app Appdelegate.m file the apple provides the code for that.
see this -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
by same method you can distinguish the iPad and iPhone.
Thank You!!

Related

How to make universal iPhone/iPad app WITHOUT SHARING VIEWS? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I realize there are a lot of questions here about making universal apps, but not so much on how to make a universal app that has completely different views and functions. I would want the user to be able to download one app, but each version is completely different. Is the only way to do this to use if statements, sensing what device the user has, and then loading the correct view controllers from there (i.e. in the delegate, load the correct first view controller)?
Thanks
Basically you want to “deploy two different apps in a single binary”. In case you already have two apps that don't share class names (or other top level object names), that should be pretty simple.
You should run device specific code as soon as possible and that is in main.m. Pass different app delegate class for iPhone and for iPad. The rest should work as normal and no classes from the other “device” should be used.
int main(int argc, char * argv[]) {
#autoreleasepool {
UIUserInterfaceIdiom idiom = [[UIDevice currentDevice] userInterfaceIdiom];
Class appDelegateClass = Nil;
if (idiom == UIUserInterfaceIdiomPhone) {
appDelegateClass = [iPhoneAppDelegate class];
}
else if (idiom == UIUserInterfaceIdiomPad) {
appDelegateClass = [iPadAppDelegate class];
}
NSCAssert(appDelegateClass, #"Unexpected idiom! Maybe iWatch?");
return UIApplicationMain(argc, argv, nil, appDelegateClass));
}
}
You could also choose different split point, for example allocating different root view controller.
In case you want to share some code after all, you can just create universal superclasses, for example iPhoneAppDelegate and iPadAppDelegate can have superclass AppDelegate that handles notifications or URL handling.
that is just the basic universal project's finish-launch method from the Apple:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
...but, you can load different view controllers for different interface idioms:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.window.rootViewController = [[UIPhoneVersionViewController alloc] initWithNibName:#"UIPhoneVersionViewController" bundle:nil];
} else {
self.window.rootViewController = [[UIPadVersionViewController alloc] initWithNibName:#"UIPadVersionViewController" bundle:nil];
}
[self.window makeKeyAndVisible];
return YES;
}
...and violá, job's done.

Multiple storyboard to manage the retina 4 and retina 3.5

Hi I'm trying to develop an app for iPhone 5 and iPhone 4/4s. I'm having trouble while using storyboard: I designed the storyboard for iPhone 4/4s, but when I try it on an iPhone 5 my GUI sucks...
I read on the internet that the easiest solution it's to use 2 storyboard: one for retina 4 and one for retina 3.5.
I wanted to ask you how I can call the different storyboard by code?
I created 2 storyboard file:
MainStoryboard.storyboard
MainStoryboardiPhone5.stroryboard
I found on internet that i should obtain the screen size of the device and load a different storyboard, but where I should do that? In Appdelegate.m in method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
To detect the size of the display I founded this code on the web:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (screenBounds.size.height == 568) {
NSLog(#"retina 4");
} else {
NSLog(#"retina 3.5");
}
return YES;
}
Now I should only find a way to invoke the different storyboard when I detect a retina 4 or a retina 3.5.
What I should do to invoke the correct storyboard?
Thank you
The iPhone 5's screen has a height of 568.
You can simply use this macro to check it its iPhone 5:
#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double)568) < DBL_EPSILON)
Then in your AppDelegate.m check for iPhone 5 and load that particular storyboard.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = nil;
if (IS_IPHONE_5) {
mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboardiPhone5" bundle:nil];
}
else {
mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [mainStoryboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
UPDATE:
Using the following macro
// Check if device is iPhone 5
#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double)568) < DBL_EPSILON)
// Get the storyboard name according to the device
#define GET_STORYBOARD_NAME(controllerName) IS_IPHONE_5 ? controllerName : [NSString stringWithFormat:#"%#-iPhone4",controllerName]
Now in your App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:GET_STORYBOARD_NAME(#"Main") bundle:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
Note:
Always the iPhone4 storyboard name should be in this format
YourStoryboardName-iPhone4.storyboard
I know it's not really what you're asking, but it's really worth persevering with AutoLayout. Unless your views are so different (eg, using different graphics, etc), AutoLayout can cope with pretty much anything by way of rearranging stuff. It's tricky to start with as it doesn't really use static positions for your layout items, it works by you telling it your intentions for how to place things relative to everything else (superview, other items, etc). Check out some tutorials online (Ray Wenderlich's one is very good).

iPad simulator Interface rotation 90°

I am developing an app (a drum machine) for the iPad, I have a draft of the user interface.
The interface is OK in iOS 6.1 simulator:
But it's turned 90° in iOS 5.1 simulator:
This is how I call the View (after some help from stackoverflow members)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[XDrumViewController alloc] initWithNibName:#"XDrumViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
How to solve it?
It is likely that you are not defining
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
which is required on iOS < 6 to make autorotation work (see here). For a quick test, define:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
in your view controller. This will enable autorotation for landscape only (i.e, it will force your controller to landscape).

How can i add an iphone interface to a ipad app so that the proper interface is selected when the app is started?

I have an ipad app and would like to make it run also on iphone (in the same app) so when i install the app on an iphone/ipad, the proper view is selected.
Honestly i don't know where to begin, could you give me some ideas of what i am dealing with?
If you look at the default project for a Universal Application, you can see how this works (see here the applicationDidFinishLaunchingWithOptions: method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Detects if it is an iPhone.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
// It's an iPhone
self.viewController = [[Test123ViewController alloc] initWithNibName:#"Test123ViewController_iPhone" bundle:nil];
} else {
// It's an iPad
self.viewController = [[Test123ViewController alloc] initWithNibName:#"Test123ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
This allows you to select the correct nib for your application's view controller based on the device.
Content_iPhone,Content_iPad are same views logic but different nibs.
so u can get load them like this
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
// load the content controller object for Phone-based devices
[[NSBundle mainBundle] loadNibNamed:#"Content_iPhone" owner:self options:nil];
}
else
{
// load the content controller object for Pad-based devices
[[NSBundle mainBundle] loadNibNamed:#"Content_iPad" owner:self options:nil];
}
[self.window addSubview:self.contentController.view];
[window makeKeyAndVisible];
}

How to start a project with both outputs iPhone & iPad?

Using the Pre Release Xcode 3.2.3 I get this as a Startup Project
(source: balexandre.com)
What should I chose / do to great a project that can have both iPad and iPhone as a target (not showing the iPhone App on iPad but a real iPad View)?
Thank you.
To avoid problems:
iPhone OS 3.1.3 is the latest release on iPhone
iPhone OS 3.2 is the latest release on iPad
My question does not infringe any agreements that me and Apple are bound to, as my question is regarding those already public Releases.
I just stated that on my Operating System I'm running the Xcode 3.2.3, that is all.
just found out a simple way:
alt text http://www.balexandre.com/temp/2010-04-17_1242.png
alt text http://www.balexandre.com/temp/2010-04-17_1241.png
Then, if you want to switch to XIB files per device, let's say, the MainView for iPhone is A and the MainView for iPad is B, in your AppDelegate you add this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// The default have the line below, let us comment it
//MainViewController *aController = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
// Our main controller
MainViewController *aController = nil;
// Is this OS 3.2.0+ ?
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// It's an iPad, let's set the MainView to our MainView-iPad
aController = [[MainViewController alloc]
initWithNibName:#"MainView-iPad" bundle:nil];
else
// This is a 3.2.0+ but not an iPad (for future, when iPhone runs with same OS than iPad)
aController = [[MainViewController alloc]
initWithNibName:#"MainView" bundle:nil];
#else
// It's an iPhone (OS < 3.2.0)
aController = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
#endif
// Let's continue our default code
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window addSubview:[mainViewController view]];
[window makeKeyAndVisible];
return YES;
}
You can choose the Application Type as "Universal" .