17

Possible Duplicate:
How to detect iPhone 5 (widescreen devices)?

Is there a way I can detect if the current device is the iphone 5? More specifically if it's using the new 4" screen?

Community
  • 1
  • 1
Hackmodford
  • 3,901
  • 4
  • 35
  • 78

4 Answers4

44

Use this

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Vladyslav
  • 583
  • 4
  • 10
17

I think you should concentrate on preferred display mode, rather than detecting iPhone5. Who knows what devices Apple will manufacture, but if your software supports that mode, it will be futureproof.

BOOL isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));

In the future, folks might want to change preferred display mode on the fly. For example disconnect AppleTV from 720p tv and plug to 1080p, without restarting the app of course.

JOM
  • 8,139
  • 6
  • 78
  • 111
bioffe
  • 6,283
  • 3
  • 50
  • 65
  • 1
    Make sure that you have the `Default-568h@2x.png` set as the launch image or it won't know to use the new size as the [UIScreen mainScreen] – mkral Sep 21 '12 at 17:50
  • 1
    @mkral mode is measured in pixels, not points – bioffe Sep 21 '12 at 17:55
  • yea I didn't see/know about that. Tested it and you're right. – mkral Sep 21 '12 at 18:05
  • note that UIScreen preferredMode is available from iOS 4.3. To get the screen size on iOS < 4.3 use this: CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGFloat screenScale = [[UIScreen mainScreen] scale]; CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale); – blejzz Dec 04 '12 at 13:14
  • @blejzz The inquiry was about iPhone 5, which came with iOS 6 preinstalled. – bioffe Dec 04 '12 at 14:53
  • @bioffe Yes but if you have a universal app you have to consider that also. – blejzz Dec 04 '12 at 20:53
4

Add this code in your initializtion:

if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
     if(UIScreenOverscanCompensationScale==1136/640){
             //move to your iphone5 storyboard
             [UIStoryboard storyboardWithName:(NSString *) bundle (NSBundle *)];
  }
     else{
             //move to your iphone4s storyboard
             [UIStoryboard storyboardWithName:(NSString *) bundle (NSBundle *)];
  }
}

This was an answer posted by me in another question here.

Community
  • 1
  • 1
SkylerHill-Sky
  • 2,106
  • 2
  • 17
  • 33
  • 1
    The above approach forces you to create two separate sets of views, which could be a real pain for the sake of consistency. If your UI changes aren't drastic, I'd really recommend just testing for the size of the screen and then resizing and potentially repositioning your objects programmatically. – The Kraken Sep 21 '12 at 20:43
  • doing that allows you to take full advantage of the bigger screen, yes you could just stretch/shrink it, but this will work. Also the overscanCompensationScale if statement works – SkylerHill-Sky Sep 21 '12 at 20:47
  • I definitely agree. Just making the point that auto-resizing UI elements that should be consistent on both devices now have to be created twice with the above approach. – The Kraken Sep 21 '12 at 20:49
2

Add this code to your application:

if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f)
    {// iPhone 5 code}
else
    {// previous version code}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179