检测硬件类型,iPhone4或iPhone5的最佳方法是什么?

时间:2012-10-24 09:24:51

标签: iphone ios objective-c cocoa-touch

我正在设置一个应用程序,我希望根据所使用的iPhone类型对视图布局进行一些更改。具体来说,我想知道垂直屏幕分辨率是1136(iPhone5)还是960(iPhone4)。我想要做的一件事是在UITableView中调整我的UITableViewCells的高度,以便在任一手机上运行应用程序时不会显示部分单元格。

我的问题是:检测正在使用的手机硬件类型的最佳方法是什么,以便我可以对视图布局进行适当的更改?

5 个答案:

答案 0 :(得分:16)

我没有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]];
}

记得导入:

#import "sys/utsname.h"

答案 1 :(得分:8)

您应该使用自动布局功能使您的视图适应屏幕的任何格式。

但是,如果您只想知道该设备是否为iPhone 5(即高度为1136像素),您可以使用:

[ [ UIScreen mainScreen ] bounds ].size.height

当然可以变成一个宏观。

答案 2 :(得分:2)

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)

在某个constant.h文件中定义此宏,并将其用于任何检查位置

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

答案 3 :(得分:2)

根据您的问题,您似乎希望根据设备和分辨率更正用户界面。在这种情况下,它很好,你可以使用下面给出的代码来识别设备。但对于非UI事物,例如寻找存在于一个设备中的设备功能,而不存在于其他设备中,建议检查该功能是否存在,而不是设备型号。 来自apple的official forum.(需要登录)

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 :(得分:1)

我一直在检查宽高比。不像其他一些方法那样通用,但对于未来的设备而言比检查特定分辨率更灵活。

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