How to detect iPhone 5 (widescreen devices)?

2018-12-31 03:09发布

I've just upgraded to XCode 4.5 GM and found out that you can now apply the '4" Retina' size to your view controller in the storyboard.

Now if I want to create an application that runs on both iPhone 4 and 5, of course I have to build every window twice, but I also have to detect whether the user has an iPhone with 3.5" or 4" screen and then apply the view.

How should I do that?

24条回答
余生请多指教
2楼-- · 2018-12-31 03:56

Relying in the size is wrong in so many levels. How about we ask to the system?

- (NSString *) getDeviceModel
{
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

Taken from Best way to detect hardware type, iPhone4 or iPhone5?, edzio27 answer.

查看更多
荒废的爱情
3楼-- · 2018-12-31 03:56

This way you can detect device family.

    #import <sys/utsname.h>
    NSString* deviceName()
    {
        struct utsname systemInformation;
        uname(&systemInformation);
        NSString *result = [NSString stringWithCString:systemInformation.machine
                                              encoding:NSUTF8StringEncoding];
        return result;
    }

    #define isIPhone5  [deviceName() rangeOfString:@"iPhone5,"].location != NSNotFound
    #define isIPhone5S [deviceName() rangeOfString:@"iPhone6,"].location != NSNotFound
查看更多
牵手、夕阳
4楼-- · 2018-12-31 03:57

Really simple solution

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
        // iPhone Classic
    }
    if(result.height == 568)
    {
        // iPhone 5
    }
}
查看更多
看淡一切
5楼-- · 2018-12-31 03:58

This has been answered a hundred times but this solution worked the best for me. It's a simple helper function and doesn't require extending a system class.

Swift 3 Helper:

func phoneSizeInInches(defaultValue: Float = 4.7) -> Float {
    switch (UIScreen.main.nativeBounds.size.height) {
    case 960, 480:
        return 3.5
    case 1136:
        return 4
    case 1334:
        return 4.7
    case 2208:
        return 5.5
    default:
        return defaultValue
    }
}

This is because it's easy to memorize a phone's inch sizes, like, "5.5 inch" or "4.7 inch" device but difficult to remember the exact pixel sizes.

if phoneSizeInInches() == 4 {
  //do something with only 4 inch iPhones
}

This also gives you the opportunity to do something like this:

if phoneSizeInInches() < 5.5 {
  //do something all iPhones smaller than the plus
}

The "defaultValue" ensures that your code will always fallback to a safe size if Apple releases a new device size and you haven't updated your app yet.

if phoneSizeInInches(defaultValue: 4.7) == 4 {
    //if a new iPhone size is introduced, your code will default to behaving like a 4.7 inch iPhone
}

Note that this is specific for phone apps, will need some changes for universal ones.

查看更多
无与为乐者.
6楼-- · 2018-12-31 04:01
if ((int)[[UIScreen mainScreen] bounds].size.height == 568)
{
    // This is iPhone 5 screen
} else {
    // This is iPhone 4 screen
}
查看更多
低头抚发
7楼-- · 2018-12-31 04:01
CGFloat height = [UIScreen mainScreen].bounds.size.height;

NSLog(@"screen soze is %f",height);

  if (height>550) {

          // 4" screen-do some thing
     }

  else if (height<500) {

        // 3.5 " screen- do some thing

     }
查看更多
登录 后发表回答