8
votes

I know you can choose ipad or iphone by using an if statement with UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad. But I was wondering if you could do the same for iPhone 3.5 inch screen to iPhone 4 inch screen. I have a game which was originally designed for the iPhone 4 inch screen, but auto layout has been used to fix the errors between the two devices, the one thing I need to change is that when

if (ball.center.y > 600) {
    RandomPosition = arc4random() %248;
    RandomPosition = RandomPosition + 36;
    ball.center = CGPointMake(RandomPosition, -22);
}

Because it was originally designed on the 4 inch screen, the game is set up to reset the ball to the top of the screen when it is > 600 pixels, which is just below the iphone 4 inch screen. It still functions properly, there is just a bit of a delay between the bottom of the 3.5 inch screen and the resetting of the position. Is there anyway I could set up an if statement with UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom(iphone4???) to set up a new ball.center.y > 300 or something?

7
maybe this link help youOmer Obaid

7 Answers

17
votes

You can try following code. Add this code in Constant.h

#define IS_IPAD (( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) ? YES : NO)
#define IS_IPHONE_5 (([UIScreen mainScreen].scale == 2.f && [UIScreen mainScreen].bounds.size.height == 568)?YES:NO)
#define IS_RETINA_DISPLAY_DEVICE (([UIScreen mainScreen].scale == 2.f)?YES:NO)

Now to check the device size add the following code in your Views

if (IS_IPAD)
{
       //do stuff for iPad
}
else
{
     if(IS_IPHONE_5)
     {
        //do stuff for 4 inch iPhone screen
     }
     else
     {
        //do stuff for 3.5 inch iPhone screen
     }

}
6
votes

you can check for 3.5 inch and 4 inch by suing below statement

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
 if ([[UIScreen mainScreen] bounds].size.height>480.0f)
 {
       NSLog(@"App is running on iPhone with screen 4 inch");
 }
 else
 {
       NSLog(@"App is running on iPhone with screen 3.5 inch");
 }
}
5
votes

Based on the other answers, if you plan to do this check a lot, it might make sense to create a macro for it:

#define isLargerPhone() (([UIScreen mainScreen].bounds.size.height > 480) && [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)

Put this in a MyMacros.h (or similar) which you can import into your Prefix.pch file, then whenever you need to perform the check you can simply do:

if ( isLargePhone()){
   // do whatever...
}
2
votes

You may use below method to check if iphone is iPhone 5 i.e of 4inch

- (BOOL)hasFourInchDisplay {
    return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0);
}
1
votes

Use this method to know device type and model like iPhone5/4S/4 or other

+ (NSString *)yesButWhichDeviceIsIt
{
    BOOL hasRetina = NO;
    if ([UIScreen instancesRespondToSelector:@selector(scale)]) 
    {
        CGFloat scale = [[UIScreen mainScreen] scale];
        if (scale > 1.0) 
        {
            hasRetina = YES;
        }
    }
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        if (hasRetina) 
        {
            return @"iPad retina";
        } else {
            return @"iPad";
        }
    } 
    else {
        if (hasRetina) 
        {
            if ([[UIScreen mainScreen] bounds].size.height == 568)
            {
                return @"iPhone5"; //4 inch 64 bit
            }
            else {
                return @"iPhone4s"; // 4 inch
            }
        } 
        else {
            return @"iPhone"; //3.5 inch
        }
    }
}

I hope this will help

0
votes

ok i'm here it is in a category, sorry to put as answer but it allows me better formatting...

first file - UIDevice+JEFkit.h

#import <UIKit/UIKit.h>

@interface UIDevice (JEFkit)
#pragma mark device type..
+(NSString *)deviceType;
+(BOOL)iPad;
+(BOOL)iPadMini;
+(BOOL)fourInchScreen;
+(BOOL)retinaDisplay;
+(Float32) pixelsPer10mm;

#pragma mark orientation..
+(UIInterfaceOrientation)orient;
+(BOOL)landscape;

#pragma mark specific feature checkers..
+(NSString *)deviceName;
+(BOOL)camera;

+(NSString *)systemVersion;
+(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement;// eg  NSString *reqSysVer = @"4.0"
+(BOOL)iOs7;



@end

and the main, several of these are just wrappers for stuff i struggle to remember, e.g. the deviceIdiom stuff, anyway its just a little 'helpers' class extension.. UIDevice+JEFkit.m

#import "UIDevice+JEFkit.h"

#include <sys/types.h>
#include <sys/sysctl.h>

@implementation UIDevice (JEFkit)


#pragma mark device type..
+(NSString *)deviceType{
  NSString *result = [[UIDevice currentDevice] model];
  return result;
}
+(BOOL)iPad{
  BOOL result = NO;
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
  if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    result = YES;
  } else {
    result = NO;
  }
#endif
  return result;


}
+(BOOL)iPadMini{

  if (![UIDevice iPad]) {
    return NO;
  }


  size_t size1;
  sysctlbyname("hw.machine", NULL, &size1, NULL, 0);
  char *machine1 = malloc(size1 + 1);
  sysctlbyname("hw.machine", machine1, &size1, NULL, 0);
  machine1[size1] = 0;

  if (strcmp(machine1, "iPad1,1") == 0 || strcmp(machine1, "iPad2,1") == 0 || strcmp(machine1, "iPad2,2") == 0 || strcmp(machine1, "iPad2,3") == 0 || strcmp(machine1, "iPad2,4") == 0 ) {
    /* iPad 1 or 2 */
    //  JLog(@"iPad 1 or 2");
    free(machine1);
    return NO;
  }

  if ([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
    if ([[UIScreen mainScreen] scale] < 2.0) {
      free(machine1);
      return YES; //all other non retina devices are eliminated
    }
  }else{
    ///does not respond to @selector(scale)
    /// should not ever happen
    free(machine1);
    return NO;
  }

  //ok only retina ipads are left...
  if (strcmp(machine1, "iPad4,4") == 0 || strcmp(machine1, "iPad4,5") == 0 ) {
    /* 2nd gen minis w retina*/
    free(machine1);
    return YES;
  }

  free(machine1);


  return NO;

}
+(BOOL)fourInchScreen{

  if (![UIDevice iPad] && ([UIScreen mainScreen].bounds.size.height == 568 || [UIScreen mainScreen].bounds.size.width == 568)) return YES;
  else return NO;
}
+(BOOL)retinaDisplay{

  BOOL result = NO;

  if ([[UIScreen mainScreen]respondsToSelector:@selector(scale)]){

    if ([[UIScreen mainScreen] scale] == 2.0) result = YES;

  }
  return result;
}

+(Float32) pixelsPer10mm{
  CGFloat dpi;
  if ([UIDevice iPadMini]) {
    dpi = (Float32)163.0;
  } else if ([UIDevice iPad]) {
    dpi = (Float32)132.0;
  }else {dpi = (Float32)163.0;}

  // JLog(@"pixels /10 mm %.3f", dpi/2.54);




  return (Float32)(dpi / (Float32)2.54);







  //return 64.173;
}

#pragma mark orientation..
+(UIInterfaceOrientation)orient{
  return [[UIApplication sharedApplication] statusBarOrientation];
}
+(BOOL)landscape{
  if (UIInterfaceOrientationIsLandscape([UIDevice orient])) {
    return YES;
  }
  return NO;

}

#pragma mark specific feature checkers..
+(NSString *)deviceName{

  return [[UIDevice currentDevice] name];
}
+(BOOL)camera{
  BOOL result = [UIImagePickerController isSourceTypeAvailable:
                 UIImagePickerControllerSourceTypeCamera];
  return result;
}


+(NSString *)systemVersion{
  NSString *result = [[UIDevice currentDevice] systemVersion];
  return result;
}
+(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement{

  // eg  NSString *reqSysVer = @"4.0";


  NSString *currSysVer = [[UIDevice currentDevice] systemVersion];

  if ([currSysVer compare:minRequirement options:NSNumericSearch] != NSOrderedAscending)
  {
    return YES;
  }else{
    return NO;
  }





}


+(BOOL)iOs7{

  return (NSClassFromString(@"NSLayoutManager") != nil);

}



@end

so to use this category i just add it to project (actually its part of my 'jefkit' static library, but if i was to add just this, id go into my .pch file and add

#import "UIDevice+JEFkit.h"
0
votes

I added a check for the longest edge since the value of the bounds changes for device orientation. I used the longest edge instead of device orientation as sometimes orientation can be reported as Unknown. I have this in a UIDevice extension:

extension UIDevice {
    func isLargerPhone() -> Bool {
      let threePointFiveInchScreenHeight: CGFloat = 480
      let dimensionToCheck = max(UIScreen.mainScreen().bounds.size.height, UIScreen.mainScreen().bounds.size.width)
      let isLargerPhone = dimensionToCheck > threePointFiveInchScreenHeight && userInterfaceIdiom == .Phone

      return isLargerPhone
    }

    func isSmallerPhone() -> Bool {
      return !isLargerPhone() && userInterfaceIdiom == .Phone   
    }
}