Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 6 years ago.
I know the in objective-c, there is a way to know which device the user is using. But I really a new of iOS development, I do not know those complex and complicated command in objective-c. Could any one tell me how to identify the phone model by coding.
Also, if I can know which device the user is using, is that possible to change the UILabel in different font size in different iOS device, such as iPhone 3.5" and iPhone 4.0"? Or is there any way to change the font position in different iOS device (autolayout is disabled)?
The way I determine what iOS device I am running on so we can change layout based on the iDevices size such as iPad
and iPhone
is like.
// iPhone 5 (iPhone 4")
#define IS_PHONEPOD5() ([UIScreen mainScreen].bounds.size.height == 568.0f && [UIScreen mainScreen].scale == 2.f && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
To get iPhone 5
you could also do
// iPhone 5 alternative
#define IS_PHONEPOD5() ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
// iPad
#define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// iPhone 4 (iPhone 3.5")
#define IS_IPHONE() (UI_USER_INTERFACE_IDIOM() == UIUserIntefaceIdiomPhone)
I add these into my xxxx-Prefix.pch
so that I can use it throughout my project without having to do an #import
or #include
. I am not saying this is the best way but this way works perfectly fine for me.
If you set these variables you can then just use if statements
like
if(IS_IPAD()) {
// Do something if iPad
} else if(IS_IPHONEPOD5()) {
// Do something if iPhone 5 or iPod 5
} else {
// Do something for everything else (This is what I do) but you could
// replace the 'else' with 'else if(IS_IPHONE())'
}
There are other ways of doing it though. Such as one developer has written some code that extends UIDevice
this can be found https://github.com/erica/uidevice-extension/
[[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"
This will allow you to detect whether the iDevice is an iPhone 3
, 3GS
, 4
and so on.
If you have any questions please just ask in the comments and I will improve my answer to include. Hope it helps if it has here is a google search for determining iDevice
You can use the below code to differentiate the devices based on the screen size and use it to change UILabel frame, font size etc...
#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define IS_IPHONE ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone" ] )
#define IS_IPOD ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPod touch" ] )
#define IS_IPHONE_5 ( IS_IPHONE && IS_WIDESCREEN )
- (void)iPhoneScreenCompatibility
{
if (IS_IPHONE_5) {
//do something
} else {
//do something }
}