Best way to detect hardware type, iPhone4 or iPhon

2020-04-04 17:16发布

I am setting up an application where I want to make a number of changes to the view layout based on the type of iPhone in use. Specifically I want to know if the vertical screen resolution is 1136 (iPhone5) or 960 (iPhone4). One of the things I want to do is adjust the height of my UITableViewCells in a UITableView so that no partial cells are displayed when the app is run on either phone.

My question is: what is the best way to detect the phone hardware type in use so that I can make the appropriate changes to my view layout?

5条回答
做个烂人
2楼-- · 2020-04-04 17:29

I've been checking aspect ratio. Not as general as some of the other methods but still more flexible for future devices than checking a specific resolution.

// 0.563380 = 16x9 (iPhone 5)
// 0.666667 = 3x2  (iPhone 4S and previous)
CGFloat aspectRatio = [UIScreen mainScreen].bounds.size.width / [UIScreen mainScreen].bounds.size.height;
// Tall Screen
if (aspectRatio > 0.55 && aspectRatio < 0.57) {
    // Tall screen stuff.
    }

    // Short Screen
} else if (aspectRatio > 0.64 && aspectRatio < 0.68) {

    // Short screen stuff.
    }
}
查看更多
倾城 Initia
3楼-- · 2020-04-04 17:43

From your question ,it seems you want to correct UI depending on device and resolution. In that case, it is fine and you can use below given code to identify the device. But for non UI things, like looking for a device capability which is present in one device, absent in other, It is always advised to check for whether that capability is present, rather than which is the device model. From apple's official forum. (login required)

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");
    }
    if(result.height == 1136){
      NSLog(@"iphone 5 resolution");
    }
  }else{
    NSLog(@"iphone standard resolution");
  }
}else{
  if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) {
    NSLog(@"ipad Retina resolution");
  }else{
    NSLog(@"ipad Standard resolution");
  }
}
查看更多
虎瘦雄心在
4楼-- · 2020-04-04 17:47

You should really use the autolayout features to make your views adapt to whatever format the screen is.

However, if you only want to know if the device is an iPhone 5 (ie has 1136px in height) you could use:

[ [ UIScreen mainScreen ] bounds ].size.height

Could ofcourse be turned into a macro.

查看更多
Explosion°爆炸
5楼-- · 2020-04-04 17:48

I dont have iphone 5 but on the other phone this code worked great:

NSMutableDictionary *devices = [[NSMutableDictionary alloc] init];
[devices setObject:@"simulator"                     forKey:@"i386"];
[devices setObject:@"iPod Touch"                    forKey:@"iPod1,1"];
[devices setObject:@"iPod Touch Second Generation"  forKey:@"iPod2,1"];
[devices setObject:@"iPod Touch Third Generation"   forKey:@"iPod3,1"];
[devices setObject:@"iPod Touch Fourth Generation"  forKey:@"iPod4,1"];
[devices setObject:@"iPhone"                        forKey:@"iPhone1,1"];
[devices setObject:@"iPhone 3G"                     forKey:@"iPhone1,2"];
[devices setObject:@"iPhone 3GS"                    forKey:@"iPhone2,1"];
[devices setObject:@"iPad"                          forKey:@"iPad1,1"];
[devices setObject:@"iPad 2"                        forKey:@"iPad2,1"];
[devices setObject:@"iPhone 4"                      forKey:@"iPhone3,1"];
[devices setObject:@"iPhone 4S"                     forKey:@"iPhone4"];
[devices setObject:@"iPhone 5"                      forKey:@"iPhone5"];

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

Remember to import:

#import "sys/utsname.h"
查看更多
ゆ 、 Hurt°
6楼-- · 2020-04-04 17:54
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)

define this macro in some constant.h file and use it any where to check

if(IS_IPHONE5)
{
// iphone 5
}
else
{
// earlier
}
查看更多
登录 后发表回答