2

I have iphone app in which I was till now successfully checking is iphone 5 with following code (in my Prefix.pch file):

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

Now when I run on my iPhone 5, I get this height in log: 480 ([[UIScreen mainScreen] bounds].size.height)

I've updated my iPhone on iOS 7. Is there some other way to check if is iPhone 5 (or height of screen)?

UPDATE: I have on my MAC OSX 10.8.5

UPDATE 2: I've figured it out that there is some bug in that project because when I create new project all examples code work.

2
  • 4
    What does it matter if you're on an iPhone 5 vs an iPhone 4s? You should be checking for the capability you want to use, not what device you're on. Sep 21, 2013 at 15:05
  • @DaveDeLong It matters if say iPhone 4 does not have enough juice to display all those nice rounded corner. Thus at run time, you check for the device and display the view accordingly.
    – Byte
    Mar 12, 2014 at 14:57

3 Answers 3

3

I just tested this code and it works checking for iPhone 5 & 4 and iOS 7 Just a bunch of pesky nested If statements!

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) {
            CGSize result = [[UIScreen mainScreen] bounds].size;
            CGFloat scale = [UIScreen mainScreen].scale;
            result = CGSizeMake(result.width * scale, result.height * scale);

            if(result.height == 960){
                NSLog(@"iphone 4, 4s retina resolution");

                //CODE IF IPHONE 4

                if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
                    //Code if iPhone 4 or 4s and iOS 7
                    NSLog(@"iPhone 4 iOS 7");
                }
            }
            if(result.height == 1136){
                NSLog(@"iphone 5 resolution");

                //CODE IF iPHONE 5

                if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
                    //Code if iPhone 5 or 5s and iOS 7
                    NSLog(@"iPhone 5 iOS 7");
                }
            }
        }
    }
5
  • I'm getting "iPhone 4 iOS 7". That's not ok.
    – iWizard
    Sep 21, 2013 at 16:51
  • @CroiOS maybe you don't have an iPhone 5 :)
    – TonyMkenu
    Sep 21, 2013 at 17:51
  • Lol. I have iPhone 5 :)
    – iWizard
    Sep 21, 2013 at 17:56
  • I just re-tested and I am really not sure why it isn't working for you as it works fine for me... Sep 21, 2013 at 18:22
  • When I create new project than it works, this is very very weird.
    – iWizard
    Sep 22, 2013 at 7:25
2

Use this :

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

isFourInches will be "YES" if the device has a four inches display, it could be an iPhone 5, 5s or 5c...

4
  • I'm getting no, something isn' ok.
    – iWizard
    Sep 21, 2013 at 16:49
  • Is your application running in horizontal orientation ? Sep 22, 2013 at 7:37
  • My application is running in portrait
    – iWizard
    Sep 22, 2013 at 8:15
  • Works for me in IOS 8, on iPod Touch 4th gen and 5th gen
    – HunTer DRF
    Oct 7, 2014 at 15:02
0

Try the condition below ...

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){   

     NSLog(@"iOS 7");          
 }
1
  • #define IS_OS_7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) Aug 3, 2015 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.