UIScreen has a new, nativeScale property in iOS 8 but the documentation does not say a word about it.
#property(nonatomic, readonly) CGFloat nativeScale
There is also a scale property but the docs say it is 2 for retina displays.
#property(nonatomic, readonly) CGFloat scale
I am wondering if there is a way to distinguish the displays. The reason why I need to know whether the device has Retina HD display is because I want to request images with different sizes based on the displays.
Thanks for any help!
Below works very well to detect type of display on iPhone6Plus.
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)] && [[UIScreen mainScreen] scale] == 3.0)
NSLog(#"Retina HD");
else
NSLog(#"Non Retina HD");
Check the below code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
NSLog(#"WIDTH:%f",screenSize.height);
NSLog(#"WIDTH:%f",screenSize.width);
float height =screenSize.height;
float width = screenSize.width;
NSString *deviceType=#"";
if (height==1136.000000 && width==640.000000)
{
deviceType =#"iPhone 5,5S and 5C Retina";
}
if (height==1920.000000 && width==1080.000000)
{
deviceType =#"iPhone 6 Plus Retina Downsample";
}
if (height==2208.000000 && width==1242.000000)
{
deviceType =#"iPhone 6 Plus Retina";
}
if (height==1334.000000 && width==750.000000)
{
deviceType =#"iPhone 6 Retina";
}
if (height==960.000000 && width==640.000000)
{
deviceType =#"iPhone 4 and 4S Retina";
}
if (height==480.000000 && width==320.000000)
{
deviceType =#"iPhone 3g and 3gs Non retina ";
}
NSLog(#"Your Result:%#",deviceType);
Reference:
iPhone Resoution
Related
Forgive me if this is a really stupid question I am just a beginner at all of this stuff. I want to make a separate interface for the iPhone 6 and 6 Plus but I don't know how the best way to be going about this is. This is a very simple app just to get myself more familiar with the language, etc. I was wondering if there is an easy way to tell if a user is using a 6 or 6 plus to make a separate interface for them. Thanks :)
Use single storyboard. Learn autolayout from raywanderlich, it think its a very good resource.
Also,now take advantage of the size classes introduced for ios 8 in xcode 6. This is called adaptive layout.
So instead of making the above four views for 3.5, 4, 4.7, 5.5 inch devices separately, and then worry about combining their respective landscape modes as well, just go for the 4 combination of size classes using auto layout (if possible, highly recommended) and relax for any other device size that may come into future.
To detect iPhone Version Please use below code.
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
if([UIScreen mainScreen].bounds.size.height == 667){
// iPhone retina-4.7 inch(iPhone 6)
}
else if([UIScreen mainScreen].bounds.size.height == 568){
// iPhone retina-4 inch(iPhone 5 or 5s)
}
else{
// iPhone retina-3.5 inch(iPhone 4s)
}
}
else if ([[UIScreen mainScreen] scale] == 3.0)
{
if([UIScreen mainScreen].bounds.size.height == 736.0){
//iPhone retina-5.5 inch screen(iPhone 6 plus)
}
}
}
Reference from Here
write this code in .pch file
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
and wwhenever you need in any controller you simply check the condition.
Below are methods that do what you asked. One major thing to look out for is in iOS7 and below, when you check [[UIScreen mainScreen] bounds] for the screen bounds, it always lists the width and height as the same no matter what orientation the phone is in. So if it's an iPhone5 in landscape mode, it will still list the width as 320 and height as 568. In iOS8, this changed, now if that same iPhone5 is in landscape, it will list the width as 568 and the height as 320. Below are methods which account for this:
+ (BOOL) deviceHasFourInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:568.0];
}
+ (BOOL) deviceHasFourPointSevenInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:667.0];
}
+ (BOOL) deviceHasFivePointFiveInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:3.0 height:736.0];
}
+ (BOOL) deviceHasScreenWithIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom scale:(CGFloat)scale height:(CGFloat)height
{
CGRect mainScreenBounds = [[UIScreen mainScreen] bounds];
CGFloat mainScreenHeight;
if ([OperatingSystemVersion operatingSystemVersionLessThan:#"8.0"])
{
mainScreenHeight = mainScreenBounds.size.height;
}
else
{
mainScreenHeight = (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ? mainScreenBounds.size.width : mainScreenBounds.size.height;
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == userInterfaceIdiom && [[UIScreen mainScreen] scale] == scale && mainScreenHeight == height)
{
return YES;
}
else
{
return NO;
}
}
Also here are the accompanying OperatingSystem class methods:
+ (NSString *) currentOperatingSystemVersion
{
return [[UIDevice currentDevice] systemVersion];
}
+ (BOOL) operatingSystemVersionLessThanOrEqualTo:(NSString *) operatingSystemVersionToCompare
{
return ([[self currentOperatingSystemVersion] compare: operatingSystemVersionToCompare options:NSNumericSearch] != NSOrderedDescending);
}
Put below lines in prefix.pch
#define iPhoneVersion ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : ([[UIScreen mainScreen] bounds].size.height == 667 ? 6 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : 999))))
Now in programming you can say...
if (iPhoneVersion==4) {
NSLog("This is 3.5 inch iPhone - iPhone 4s or below");
} else if (iPhoneVersion==5) {
NSLog("This is 4 inch iPhone - iPhone 5 family");
} else if (iPhoneVersion==6) {
NSLog("This is 4.7 inch iPhone - iPhone 6");
} else if (iPhoneVersion==61) {
NSLog("This is 5.5 inch iPhone - iPhone 6 Plus.. The BIGGER");
} else {
NSLog("This is iPad");
}
However, I would say use one storyboard for all iPhones by learning autolayout.
I ma in trouble with detecting iPad. I have a code for detecting if its iPhone 4/4s, iPhone 5/5s or iPad mini. But I have no idea how to detect iPad air(1536 x 2048). If I use this code it returns me for iPad retina in simulator the settings I set for iPhone 5.
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
if([UIScreen mainScreen].bounds.size.height == 568){
//iPhone5
} else{
//Iphone4
}
}else {
//iPadmini
}}
Finally fixed it!
#define iPhone4Or5oriPad ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : 999))
if (iPhone4Or5oriPad==4) {
NSLog(#"This is 3.5 inch iPhone - iPhone 4s or below");
} else if (iPhone4Or5oriPad==5) {
NSLog(#"This is 4 inch iPhone - iPhone 5 & above");
} else {
NSLog(#"This is iPad");
}
I know,
We can detect retina device by either of the following methosds:
BOOL isRetina;
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)] && [[UIScreen mainScreen] scale] == 2){
isRetina = YES;
} else {
isRetina = NO;
}
or
if ([[UIScreen mainScreen] respondsToSelector:#selector(displayLinkWithTarget:selector:)]){
NSLog(#"scale = %f",[[UIScreen mainScreen] scale]);
if ([[UIScreen mainScreen] scale] > 1.0) {
NSLog(#"Retina Display iPad3");
}
else
{
NSLog(#"Non Retina Display iPad 1/2");
}
}
But when i run this code into my ipad 2 which has ios 7.1, i am getting , it is non-retina device and when i run this code into ipad retina device's simulator it is working perfectly there.
Any guideline?
The code is correct. The iPad 2 does not have a Retina screen.
I am using cocos2d in iOS .
I am having a problem detecting the iphone5 resolution(1136*640).
I am using code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
When we run app on iPhone simulator (4 inch), it gives screenWidth and screnHeight-320,480 respectively.
Can anyone tell me the solution for this?
Write this code in .m file.
#define WIDESCREEN ( fabs( ( double )[ [UIScreen mainScreen ]bounds ].size.height-( double )568 ) < DBL_EPSILON )
Now you can know the screen size like this.
if(WIDESCREEN)
{
NSLog(#"iPhone5 is here");
}
else
{
NSLog(#"iPhone4 is here");
}
you can add checks in your code for iPhone 4inch screens and 3.5 inch screens
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone Classic
// set your frames for classic iPhone here
}
if(result.height == 568)
{
// iPhone 5, iPhone 5S, iPhone 5C
// set your frames for 4 inch iPhone here
}
iPhone 5 resolution is 1136*640 only. If you wanna check just Log below Code
the resolution it will give exactly (1136*640)
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
NSLog(#"Screen SIze %#",NSStringFromCGSize (screenSize));
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width >= 568 || [[UIScreen mainScreen] bounds].size.height >= 568)?YES:NO
#define IS_IPHONE4 ([[UIScreen mainScreen] bounds].size.width >= 480 || [[UIScreen mainScreen] bounds].size.height >= 480)?YES:NO
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if(IS_IPHONE4)
{
// iPhone 4
}
if(IS_IPHONE5)
{
// iPhone 5
}
}
I know cocos2d can take into consideration whether code is run on iphone or ipad using
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
}
But can I write cocos2d code takes into consideration both sizes of the iphone screen. For Example:
#define xPosition1 120.0
#define xPosition2 240.0
#define xPosition3 360.0
if (iphone5) {
#define xPosition1 142.0
#define xPosition2 284.0
#define xPosition3 426.0
}
One approach is to use the following:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
This will return you the screen width and height depending on your device. That means, the iPhone 4S will return 320 x 480 and iPhone 5 will return 320 x 568