How can I check what iOS device the user is using? [duplicate] - ios

This question already has answers here:
Detect iOS device type
(5 answers)
Identify new iPhone model on xcode (5, 5c, 5s) [duplicate]
(6 answers)
Closed 9 years ago.
How can I find out what device a user is using? The code I currently use is:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if(screenBounds.size.height == 568){
NSLog(#"User is using an iPhone 5s, 5c, or 5");
}
else{
NSLog(#"User is using an iPhone 4s or earlier");
}
What other numbers could this return, and what device would it be? For example, I was hoping for something like this:
screenBounds.size.height == 568 would be an iPhone5/5s/5c
screenBounds.size.height == 480 would be an iPhone 4/5s
screenBounds.size.height > 570 would be an iPad
and so on. I'm going to be using this to change the nib file based on what device the user is using, so that I won't have to move every single button, image, label, or anything else with CGRectMake.
I'm not using auto layout because I would also like to have some more customization based on what device the user is using.

To check the kind of device:
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
// You are using iPad
return YES;
}
else if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomiPhone )
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if(screenBounds.size.height == 568){
NSLog(#"User is using an iPhone 5+");
} else{
NSLog(#"User is using an iPhone 4s-");
}
}

Related

How to rotate a photo on iPhone only?

I have an iPhone app I am testing on my iPhone and iPad. When running my app adhoc, the problem is that when the photo loads on my iPhone 4 during ad hoc testing, the photo loads on its side; the photo loads fine on iPad mini.
The following line of code successfully rotates the fullscreen image on iPhone, but conversely rotates the image when I test on iPad.
//successfully rotates but also over rotates on iPad
PhotoView.transform=CGAffineTransformMakeRotation(M_PI*0.5);
Can someone help me set up conditional statements so I can use my CGAffineTransforMakeRotation line of code only on iPad?
I have so far tried the following conditional statements but they don't work for some reason: i.e. only the iPhone if statement works and overrides the iPad if statement so the iPhone statement is carried out on both devices.
//does not successfully set the bk for iPad but does for iPhone. The iPad statement is overridden and the iPhone statement is carried out on both platforms. Any ideas?
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
}
This second conditional statement also doesn't work...
if ([[UIScreen mainScreen] bounds].size.height == 480)
{
// iPhone, iPod Touch
}
if ([[UIScreen mainScreen] bounds].size.height == 568) {
// iPhone 5
}
if ([[UIScreen mainScreen] bounds].size.height == 667) {
// iP6
}
if ([[UIScreen mainScreen] bounds].size.height == 736) {
// iP6+
}
if ([[UIScreen mainScreen] bounds].size.height == 1024) {
// iPad
}
My question: does anyone have code to rotate an image on iPhone but not iPad?
If it's an iPhone app, it is always "on an iPhone". Even when running on the iPad, it is running on a virtual iPhone in emulation mode. It is impossible for it to behave differently on the different devices; it is running on just one device type, as far as it can tell.
If you don't like that, make it a Universal app. Now it runs natively on iPad and your tests will work.

Auto Layout iphone 4 - 6plus

Since apple has now introduced the iPhone 6 + 6 plus. How would I make the image for different sizes of phones? For iPhone 4 and 5/5s/5c you would do
#define IS_568_SCREEN (fabs((double) [[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSLION)
if(IS_568_SCREEN) {
//Iphone 5/5s/5c
} else {
//iphone 4s }
But how would you do it for iPhone 4s to iPhone 6 plus? Also my auto layout isn't working very well. Im trying to change the size for all my objects dependent of the phone and using auto layout to place them correctly. However its not working out, I'm doing "height", "leading space to Container Margin", "Bottom Space To Bottom layout" and "Trailing space to container". As this will be a little hard and time consuming for all the objects sizes, can I use multiple storyboards? and what I mean by that is can I have one storyboard file for iPhone 6, then one for iPhone 6 plus etc. Or if not whats the proper way of doing auto layout on the interface builder?
Here's the answer to what I was looking for, nothing complicated :)
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
if (screenHeight == 480) {
// do iPhone 4s stuff
} else if (screenHeight == 568) {
// do iPhone 5, 5s, 5c stuff
} else if (screenHeight == 667) {
// do iPhone 6 stuff
} else if (screenHeight == 736) {
//do iPhone 6 plus stuff
}

Detect iPhone specific application 'is running' on iPAD

I have an app which is developed for iPhone only. When user runs it on iPAD
it is not showing properly. I know how to detect "IS_iPAD" when project is universal but
when project is iPhone specific the USER DEVICE is always returned as iPhone.
How one can detect if iPhone specific application is run on iPAD?
For sake of completion here is the #MichaelPatzer answer pointed by #user42949
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
}
You can use this different macro to know if you are using iPhone, iPhone 5 or iPad :
#define IS_IPHONE() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE5() ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) && [UIScreen mainScreen].bounds.size.height == 568)
Another option, if your code is layout-specific (which is how I got here):
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat screenRatio = MIN(screenSize.width / screenSize.height, screenSize.height / screenSize.width); // account for rotation
if(screenRatio > 6.65 && screenRatio < 6.67) {
// iPad is 6.66666
}

Xcode - How do I Detecting screen size of iPhone [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have two xibs, one for the iPhone 4 and one for the iPhone 5; 3.5 inch and 4 inch. I simply want to put some code that tells the app which to load. I have this code which is supposed to do the job:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone))
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
//Load 3.5 inch xib
}
if(result.height == 568)
{
//Load 4 inch xib
}
I put it in the ViewController.M(is that where I put it?) and the build fails saying there is a parse issue expected identifier of "("
Can anyone help me with this simple fix? Thanks!
add below line in your prefix.pch file...this is the simplest way to check screen size, no need to make extra lines of code...
#define IsIphone5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
now whereever you want to check screen size , just make condition like below, and you can do whatever you want....
if(IsIphone5)
{
//your stuff
}
else
{
//your stuff
}
Happy Coding!!!!
put this condition
if ((int)[[UIScreen mainScreen] bounds].size.height == 568)
{
// This is iPhone 5 screen
} else {
// This is iPhone 4/4s screen
}
remove extra ")" at the end from this line
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone))
Try this remove last Bracket
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
//Load 3.5 inch xib
}
else if(result.height == 568)
{
//Load 4 inch xib
}
}

How to detect if the device is an iPhone 5? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to detect iPhone 5 (widescreen devices)?
I am trying to add a new view to an existing ios4 project to handle the new iphone5 screen size.
However I dont have an iphone here to test on and the code I am using to test the screen size isnt working, just wondering if there is another way of detecting the device type??
NSLog(#"%f", [ [ UIScreen mainScreen ] bounds ].size.height);
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if([UIScreen mainScreen].bounds.size.height == 568.0)
{
//move to your iphone5 storyboard
[self changeView:splashScreenBIGV viewH:splashScreenH animLength:SCREEN_CHANGE_ANIM_LENGTH];
}
else{
//move to your iphone4s storyboard
[self changeView:splashScreenV viewH:splashScreenH animLength:SCREEN_CHANGE_ANIM_LENGTH];
}
}
For my universal app, Forex App, I'm simply using the screen height detection concept like so:
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if (screenSize.height > 480.0f) {
/*Do iPhone 5 stuff here.*/
} else {
/*Do iPhone Classic stuff here.*/
}
} else {
/*Do iPad stuff here.*/
}
Answer is here. Basically, you don't. Use the new constraint layout system in iOS 6. You'll be chasing your tail if you have to rev your layout for every new device with a different screen size/resolution.

Resources