1

I know it's not a great idea to alter an app based on the screen size but I am trying to modify the camera and use a custom overlay image so I need to know the screen size. There are a bunch of solutions to detecting the screen size using macros etc I am using this:

-(void)detectPhone{

    CGFloat width;
    width= [[UIScreen mainScreen] bounds].size.width;

    if(width==320) {

        CGFloat height;
        height = [[UIScreen mainScreen] bounds].size.height;

        if(height==480) {

            NSLog(@"iPhone 4/4s");
        }

        else {

            NSLog(@"iPhone 5");
        }

    }

    else if (width==375) {


        NSLog(@"iPhone 6");

    }


    else {

        NSLog(@"iPhone 6+");

    }



}

Is there anything wrong with using this method or any situations where it wouldn't work? Ran it through the simulator seems fine. Any pointers would be appreciated. Thanks

Kex
  • 8,023
  • 9
  • 56
  • 129

4 Answers4

3

Its fine this way, but you are comparing floats, so I would avoid using == for that, instead use >

IxPaka
  • 1,990
  • 1
  • 16
  • 18
  • Even this hint is helpful in general, an object of a floating point number can represent every "small" integral value, so OP's code should work in relationship to that. – Amin Negm-Awad Mar 19 '15 at 10:00
1

I use this :

    #define IS_IPHONE [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone
    #define PJ_SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
    #define PJ_SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
    #define PJ_SCREEN_MAX_LENGTH (MAX(PJ_SCREEN_WIDTH, PJ_SCREEN_HEIGHT))
    #define PJ_SCREEN_MIN_LENGTH (MIN(PJ_SCREEN_WIDTH, PJ_SCREEN_HEIGHT))
    #define IS_HEIGHT_GTE_568 PJ_SCREEN_MAX_LENGTH >= 568.0f
    #define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))
    #define IOS7_DELTA_HEIGHT (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) ? 20:0)
    #define IS_IOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    #define DEVICE_SIZE [[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size

If you want to check device model, take a look at: Determine device (iPhone, iPod Touch) with iPhone SDK

Community
  • 1
  • 1
Kilogen9
  • 173
  • 1
  • 7
0

I think you will have problems with iOS8. From iOS8 [UIScreen mainScreen].bounds
is interface-oriented (look at session "View Controller Advancements in iOS 8" of WWDC 2014), this means that in landscape mode on iOS8 height and width are reversed.

You can create a helper with a code like below:

CGRect bounds = [UIScreen mainScreen].bounds;
if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
    bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
    //Your code to detect the device
    ...

}
Andr3a88
  • 679
  • 5
  • 13
0

You should have the screen for one time only then you can use it in your entire app (whenever you need). For this you can have a variable CGSize deviceSize in AppDelegate.h and in AppDelegate.m in your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in first line of this method, you can get the device size like this,

deviceSize = [[UIScreen mainScreen] bounds].size;

and you can define a function like this,

- (CGSize) sizeOfDevice {
    return deviceSize;
}

add it in AppDelegate.h like this,

- (CGSize) sizeOfDevice;

then you can use it with your delegate object.

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • `"You should have the screen for one time only"` this isn't true. What if the screen size changes? Which it will when you rotate. – Popeye Mar 19 '15 at 09:39
  • Yes, you're right. But OP said [this](http://stackoverflow.com/questions/29140151/is-it-okay-to-use-this-code-to-detect-the-iphone-screen-size/29140998?noredirect=1#comment46504722_29140442) – Hemang Mar 19 '15 at 09:47
  • That statement is still wrong though. You don't need to take into consideration whether it rotates or not here because if they have set the app up correctly it will be locked in portrait if they only want portrait so you don't need to worry about it. – Popeye Mar 19 '15 at 09:51