简体   繁体   English

检测硬件类型(iPhone4或iPhone5)的最佳方法?

[英]Best way to detect hardware type, iPhone4 or iPhone5?

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. 我正在设置一个应用程序,该应用程序要根据使用的iPhone类型对视图布局进行一些更改。 Specifically I want to know if the vertical screen resolution is 1136 (iPhone5) or 960 (iPhone4). 具体来说,我想知道垂直屏幕分辨率是1136 (iPhone5)还是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. 我想做的一件事是在UITableView中调整UITableViewCells的高度,以便在任一手机上运行该应用程序时都不会显示部分单元格。

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? 我的问题是:检测使用中的电话硬件类型的最佳方法什么,以便我可以对视图布局进行适当的更改?

I dont have iphone 5 but on the other phone this code worked great: 我没有iPhone 5,但在另一部手机上,此代码效果很好:

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"

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: 但是,如果您只想知道该设备是否为iPhone 5(即高度为1136像素),则可以使用:

[ [ UIScreen mainScreen ] bounds ].size.height

Could ofcourse be turned into a macro. 当然可以变成一个宏。

#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 在某个constant.h文件中定义此宏,并在任何要检查的地方使用它

if(IS_IPHONE5)
{
// iphone 5
}
else
{
// earlier
}

From your question ,it seems you want to correct UI depending on device and resolution. 从您的问题来看,您似乎想根据设备和分辨率来更正UI。 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. 但是对于非UI事物,例如查找一个设备中存在的设备功能,而寻找另一设备中不存在的设备功能,则始终建议检查该功能是否存在,而不是检查哪个设备型号。 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");
  }
}

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.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM