Detect device and accordingly loads UI - iphone

Just finished up my iPhone appln, now wants to use same appln for creating an iPad version. What i really want to do is to detect which on which device the app is running and accordingly pick UI at runtime. I get some code regarding which is my current device. Basically main idea behind is that is i don't want to write the server-client communication part again for separate ipad version. Ui is different hence I don't want to create a universal app for this.
My Prob: What settings should I do to make to work the application as described above. Currently when I run using iPhone Simulator, It says tht my current device is iPhone. But, when I changed my device to iPad Simulator and than run it back again, it convert that int iphone simulator.
Thanks.

you can check with this line:
NSString *model = [[NSString alloc]initWithString:[[UIDevice currentDevice] model]];
model value would be either iPhone or iPad
or you can go with [[UIDevice currentDevice] userInterfaceIdiom] and compare the values with
typedef enum {
UIUserInterfaceIdiomPhone,
UIUserInterfaceIdiomPad,
} UIUserInterfaceIdiom;

Related

can i release an app which targets iPhone for the iPad?

I'm working on an iPhone app, and decided to look into making it universal. If I set my build settings to target iphone/ipad, a lot of sizes and alignments get buggered (as I expected), and just generally it doesn't look good.
If I only target iPhone, but run it on the iPad, and hit the 2x button, it looks great. I'd like to release my app on the app store for the iPad, despite leaving the targeted platform as the iPhone since it looks and works much better.
I'm wondering if that will be an issue when I submit? Can you only release apps for iPad on the store if they target iPad?
Thanks!
If you develop for the iPhone you will want to use interface "xib" files that have all the correct sizing for the iPhone/iPod. iPad will be allowed to use the application
In your plist you will have the option of giving a startup xib file [NSMainNibFile] for the application to start with.
Or you can alternatively include [NSMainNibFile~ipad] and compile it for Universal. This will tell the ios that it should open NSMainNibFile for the iPhone/iPod and should open NSMainNibFile~ipad for the iPad.
There are a number of other settings that would need to be set for the ipad's icon, default screen among other items. But the Nib file settings are the most needed.
then you have separate xib file's for each platform. Conforming to the Model-View-Controller setup it would be relatively simple to attach your new view(xib) to your existing controller(.h/.m) and wire up a new look to your code and make an entirely new app..
Some sections of code will need to be changed dependent on weather you are on the iPad or the iPod.
(e.g. Layout, Special iPad/iPod only features etc.)
Here is the code I use do determine if I am on the iPad.
#define IS_IPAD() ([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)] ? \
[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad : NO)
Some developers dont comply totally with the MVC paradigm so your project may require some retooling to be able to function on both platforms. But if you are able to pinpoint the places that differentiate from one platform to another then your application will be able to function seamlessly on both platforms. (Likely with a better layout on the iPad as there is more real estate do play with when laying out your controls)
Alternatively if you want the application to be iPhone style but run on the iPad, It already does that by default.
If you target the iPhone (so that it will appear in 1x/2x mode on the iPad) it will only appear in the iPhone App Store.
iPad Users will still be able to download the app, but it will be in the iPhone Apps section.
So unless you explicitly target the iPad, it won't appear on the iPad store. You could consider redoing the graphics and alignments for the iPad app and sell it as a HD app.
If you need it to be universal look at doing something like this:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
controller = [[MyController alloc] initWithNibName:#"MyiPadNib" bundle:nil];
else
controller = [[MyController alloc] initWithNibName:#"MyiPodNib" bundle:nil];
You application will only be viewable in the iPad section of the store if it specifically targets iPad (is either iPad only, or a universal application).
What you are talking about is an iPhone application running on an iPad: where you are opting to only target iPhone devices. These apps will not be shown in the iPad section of the store.
Think of it as an incentive by Apple to get you to design an app that behaves well on iPad and iOS.
Remember, iPad users can still download iPhone only apps through the store, they're just in a separate section.

How to make an iphone and iphone4-retina compatible app (done in cocos2d) easily adapted to ipad?

My question is simple:
1. I made an iphone app all done in cocos2d.
2. I adapted it to iphone4-retina, with all PNG files have their -hd copy.
Run in iPhone, the app displayed correctly (320x480).
Run in iPhone4-retina, the app also displayed correctly (640x960).
But run in iPad, if the app is set to iPhone only, it run correctly, but only as iPhone resolution (320x480). If the app is set to iPhone/iPad, of cause it will display wrong.
I'm sure there should be an option that force a retina-compitable iPhone app also run as retina on iPad (apple guys cannot be silly enough to miss it), but I just couldn't find it.
Where's the option? Or, is there an alternative that cocos2d has a same-easy switch to do the job? I do not need suggestions such as using relative coordinates or anything requires modifications more than twenty code-lines.
As far as I know, there is no automatic adaptation of an iPhone app to iPad. You still should be able to create a universal app for both iPhone and iPad and then re-create your UI based on whether you are running on one device or the other.
Specifically, you could:
Create an XCode project for a universal app (armv6 and armv7, targeting iPad and iPhone) and import there your existing project (source, resources, settings).
1b. (you could modify your existing project, but this could be trickier to do correctly.)
add icons and default images as per iPad guidelines in addition to those you have for iPhone;
As to the rest, you could follow a similar approach to the one highlighted here for Xibs:
test for iPad:
+(Bool)isIpad{
return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
}
translate image name before loading:
+(NSString*)properImageFileName:(NSString*)imageName {
if ([xxxx isIpad]) {
return [NSString stringWithFormat:#"ipad_%#", imageName];
} else {
return imageName;
}
}
Thus, if you are on iPhone, image names are not changed and will follow the iPhone convention for retina display; if you are on iPad, you change the name on the fly and use the right image. (of course, you can use the convention you prefer to identify iPad images).
This should make it pretty trivial, but keep in mind the size toll that having all the images in your universal app tolls.

iOS Development: How can I prevent an iPad from running a universal app in iPad mode?

I'm diving into iOS development and I created a universal app that turned into an iPhone-only app. When it runs on the iPad, it just loads a white screen since there's no iPad code written yet. What I'd like is for it to run in "iPhone" mode on the iPad, if it somehow ends up on an iPad. I have the "Targeted Device Family" property set to "iPhone", so that should prevent it from showing up in the App Store as an iPad app, but if anyone owns both an iPad and an iPhone, then the app could end up synced to the iPad, at which point it will just load the white screen because it will try to run the app in iPad mode, which it doesn't have any code to support. In this situation, I prefer that it actually ran on the iPad, but in iPhone mode.
My questions are...
When an iPad runs a universal app, how does it know to run it in "iPhone mode" or execute the iPad specific code?
In a universal app, how does it know which code is iPhone and which code is iPad?
How can I prevent the iPad from trying to run the iPad code and, instead, run the iPhone code?
I apologize if I sound like a total noob, but I am. Thanks so much for your wisdom!
The iPad looks into the application's Info.plist, for the UIDeviceFamily key, which is an array. The value '1' indicates iPhone/iPod Touch, '2' indicates 'iPad'. If there's a '1' but no '2' then you get the simulated iPhone environment. This value is written to the Info.plist automatically as a result of your 'Targeted Device Family'. If you think you've set it to iPhone but are still getting an iPad build, check you didn't just set it for one build configuration. Check the Info.plist within your produced app bundle if you want to be really confident.
There's only one binary, which you write to launch correctly on either device.
Just don't target the iPad.
I'm assuming what you actually want is to remove the "universal" capability, and just make it an iPhone app.
In Xcode, go to Project => Edit Project Settings => Build.
Search for universal, or 'Targeted Device Family'.
Pick iPhone.
Goodbye iPad.
When an iPad runs a universal app, how does it know to run it in "iPhone mode" or execute the iPad specific code?
The iPad looks for the Targeted Device Family, if the iPad is not present, then it knows it must run the app in iPhone mode.
In a universal app, how does it know which code is iPhone and which code is iPad?
When you write the code for the app, you must specify what device you are targeting if there are specific things you need to do per device. (see the code example below)
How can I prevent the iPad from trying to run the iPad code and, instead, run the iPhone code?
Do not support iPad in your Targeted Device Family. Second, in your code, do not specify that specific code needs a specific device, for example:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
/* run something specific for the iPad */
}
else
{
/* run something specific for the iPhone */
}
If you build an universal app, it will use your iPad code. It is not possible to run a universal app in "iPhone Mode". Apple will check that you have followed the iPad design specifications.
In a universal app, there are two app-delegates: AppDelegate_iPhone.h and AppDelegate_iPad.h
You can add your iPhone code in the AppDelegate_iPad, but Apple will not be pleased.
You should NOT add this to your Info.plist file. Instead, add it to your build settings per Apple's suggestion. Specifically, use the TARGETED_DEVICE_FAMILY build setting.
If you are using storyboards, you also want to remove the UIMainStoryboardFile~ipad key from your Info.plist as it will be used regardless of your TARGETED_DEVICE_FAMILY setting.
Good luck!
I think there is an entry in the info.plist file for each of the devices that says which main window to load.
Maybe a quick and dirty solution would be to set both MainWindow-iPhone and MainWindow-iPad to the same -iPhone- main window.
Another way to do it (with code) is:
In your App's AppDelegate (if your App was created as an Universal App) you can find the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
//iPad...
} else {
//iPhone and iPod Touch...
}
return YES;
}
There you can customize what view to show.
Since Xcode 5, you can chose your development target devices from the Project:
From the devices section within Development Info, now you can choose:
1-iPhone 2- iPad 3- Universal
I think that something is wrong with your configuration, because if you target the code for iPhone only Device, the app will bu runnable on an iPad with the screen that was designed for iPhone (so, reduced, with the possibility to x2).
the proposals from the AppStore (iPhone/iPad/both) depend on the user's preferences
you can experiment it with the Simulator (and choose iPad as the Device)
the code is the same for iPad/iPhone ! ... unless you use [[UIDevice currentDevice] userInterfaceIdiom] mentioned supra...

How to detect my iPhone app is being run on an iPad

I have an opengl application that renders better in RetinaDisplay mode (double scale factor) and I noticed the iPad emulates an iPhone app with a low resolution screen (normal scale factor).
I want to double the scale factor when my iPhone app is run on an iPad, in order to benefit from Retina Display graphics. But it seems the iPad really well fakes being an iPhone (which would be perfect if only it was a Retina Display one...)
When I force the double scale, it works very well (at least in simulator, I do not have an iPad to test).
So I need a way to know if I am run on an iPad despite many things telling me it to be an old iPhone.
Or maybe I should not try to do that ?
If the app is an iPhone app running in the emulator mode on an iPad, it will have a userInterfaceIdiom of Phone, but a model type of iPad. You can check this with the following code:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
[[[UIDevice currentDevice] model] hasPrefix:#"iPad"]) {
// This app is an iPhone app running on an iPad
}
If you are looking to make custom code (most likely custom UI related methods) for the iPad only then you can use (as Apple directs) the UI_USER_INTERFACE_IDIOM() method that exists in iOS 3.2 and later
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
}
else
{
// The device is an iPhone or iPod touch.
}
You can read more here http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BuildTimeConfiguration/BuildTimeConfiguration.html
This is the Apple recommended method
Look up in the documentation, UIDevice:
For instance something like:
NSString *system = [[UIDevice currentDevice] systemName];
Then by using [system isEqualToString:#"iPad"] whether its an ipad or not.
UIDevice is a very nice class, it also has stuff like multiTaskingSupported, systemVersion etc. gotta love UIKit ;)
i think it is that:
// Set hello to "Hello, <device or simulator>"!
if TARGET_IPHONE_SIMULATOR
NSString *hello = #"Hello, iOS Simulator!";
else
NSString *hello = #"Hello, iOS device!";
endif
the link apple doc
about
This actually will only tell you if
the app is being run in a simulated
environment, or on an actual device,
and has no influence on whether the
platform is iPad or iPhone.
In fact it says at compile time the target of the platform you are compiling for, thus before run you know and do the necessary for take care of something specific.
For example I have diferent URL for developing (running on simulator) and for production usage, so I do some like
#if TARGET_IPHONE_SIMULATOR
#define URL #"http://192.x.x.x/request"
#else
#define URL #"http://example.com/request"
#endif
you shouldn't be able to tell the difference, if its an iPhone app, then as far as it can tell it is running on an iPhone. if you want to target an iPad, then you need to build it for an iPad target.

Does UI_USER_INTERFACE_IDIOM work with Targeted Device Family

So I'm currently using UI_USER_INTERFACE_IDIOM in conjunction with [[UIDevice currentDevice] model] to check if I'm on an iPhone, iPod or iPad. What I've found is that in the iPad simulator 3.2, UI_USER_INTERFACE_IDIOM still evaluates to UIUserInterfaceIdiomPhone (iPhone).
I'm wondering if this has something to do with my Targeted Device Family setting. I'm only targeting iPhone for my App (I don't want to make a universal app with scaling views). However, I support the 3.2 SDK so I still want users that have an iPad to be able to run my iPhone app.
Will UI_USER_INTERFACE_IDIOM evaluate correctly on the iPad even when I'm targeting iPhone?
UI_USER_INTERFACE_IDIOM does not check if the device is an iPhone or iPad. What it checks is whether the user interface is in iPhone mode (the 1x/2x thing) or iPad mode.
If an app isn't configured to target iPad, it will always return UIUserInterfaceIdiomPhone because the UI is an iPhone app. It is a feature by design.
And even if the app is configured to target iPhone only, the iPad should be able to run it without any problems as long as you use the methods as documented.
(If you need iPad-specific capabilities, do not check whether the device really is an iPad. Instead, check for individual capability.)
Best I can offer is that on the iPad simulator (3.2) while running in "iPhone" mode the
NSLog(#"model : %#", [UIDevice currentDevice].model);
returns
model : iPhone Simulator
(as a note: I am building for "iPhone" only and thus running in the iPhone experience on iPad. I have to assume the "model" name returned is affected by that)
As people have said, check the individual capability.
For making a call, do this
// Only show the button if its is a device capable of making calls
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"tel:"]]) {
self.Button.hidden = NO;
} else {
self.Button.hidden = YES;
}