+8 votes
925 views
in Programming by (11.7k points)
I just upgraded to XCode 4.5 GM and discovered that you can now apply the "4" Retina "size of your view controller on the storyboard.

Now if I want to create an application that runs on the iPhone 4 and 5, of course I have to build all the windows on two occasions, but I have to detect if the user has an iPhone with a 3.5 "or 4" screen and then apply the view.

How should I do that?

1 Answer

+8 votes
by (11.7k points)
edited by

First of all, you should not rebuild all their views to fit a new screen, or the use of different views for different screen sizes.

The use of auto-resizing capabilities of iOS, so their opinion can adjust and adapt to any screen size.

That is not very difficult to read some documentation about it. This will save a lot of time.

iOS 6 also offers new features on this but this is still the NDA at the time. 
Be sure to read the list of changes API Apple Web Developer, if you can access it.

Edit : As iOS 6 already, check the new design Automatic capabilities.

That said, if you really need to detect the iPhone 5, you can simply rely on the size of the screen .

[[UIScreen mainScreen]  bounds ] .size .height

The iPhone 5 is the screen has a height of 568. 
You can imagine a macro to simplify all this:

# define IS_IPHONE_5 (fabs ((double) [[UIScreen mainScreen] bounds] .size.height - (double) 568) <DBL_EPSILON)

Using fabs with epsilon is here to avoid mistakes precision floating point when compared, as noted in the comments by H2CO3.

So from now on you can use the if / else norm:

if (IS_IPHONE_5)
{}
else
{}

Edit - Better detection

As stated by some people, this not only detect a screen , not a real iPhone 5.

The next version of the iPod touch may also have a screen, so you can use another set of macros.

Let's rename the original macro IS_WIDESCREEN :

# define IS_WIDESCREEN (fabs ((double) [[UIScreen mainScreen] bounds] .size.height - (double) 568) <DBL_EPSILON)

And we will add detection model macros:

# define IS_IPHONE ([[[UIDevice currentDevice] model] isEqualToString: @ "iPhone"]) 
# define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString: @ "iPod touch"])

In this way, we ensure that we have a model of iPhone and a screen, and we can redefine theIS_IPHONE_5 macro:

# define IS_IPHONE_5 (IS_IPHONE && IS_WIDESCREEN)

Also note that, as said @ LearnCocos2D, that macros do not work if the app is not optimized for iPhone 5 screen (missing [email protected] image), as the size of the screen will still be 320x480 in this case.

I do not think this can be a problem because you do not see why we would want to detect an iPhone 5 in a non-optimized application.

IMPORTANT - iOS 8

In iOS 8 bounds of ownership UIScreen class now reflects the orientation of the device . 
So obviously, the above code does not work out of the box.

To fix this, you can simply use the new nativeBounds of the property, instead of bounds , as it will not change the orientation, and as based on a portrait mode. 
Note that the dimensions of nativeBounds is measured in pixels, so that for an iPhone 5 the height will be 1136 instead of 568.

If you are also targeting iOS 7 or less, be sure to use feature detection, such as calling nativeBoundsbefore iOS 8 lock your application:

if ( [[UIScreen mainScreen]  respondsToSelector : @ selector ( nativeBounds)])
 {
     / * Detect using nativeBounds - iOS 8 and Greater * /
}
else 
{
     / * Detect using bounds - iOS 7 and lower * / 
}

You can adapt the above macros follows:

#define  IS_WIDESCREEN_IOS7 ( fabs (( double ) [[UIScreen mainScreen]  bounds ] .size .height  - ( double ) 568) < DBL_EPSILON )
 #define  IS_WIDESCREEN_IOS8 ( fabs (( double ) [[UIScreen mainScreen]  nativeBounds ] .size .height  - ( double ) 1136) < DBL_EPSILON )
 #define  IS_WIDESCREEN       (( [[UIScreen mainScreen]  respondsToSelector : @ selector ( nativeBounds)]) IS_WIDESCREEN_IOS8: IS_WIDESCREEN_IOS7)

And obviously, if you need to detect an iPhone 6 or 6 Further, using the corresponding display sizes.

Final Note

Comments and suggestions have been incorporated into this post. 
Thank you all.

Ask a Question
Welcome to WikiTechSolutions where you can ask questions and receive answers from other members of the community.

You can ask a question without registration.

Categories

...