【问题标题】:Best way to detect hardware type, iPhone4 or iPhone5?检测硬件类型的最佳方法是 iPhone 4 还是 iPhone 5?
【发布时间】:2012-10-14 07:45:32
【问题描述】:

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

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

【问题讨论】:

标签: iphone ios objective-c cocoa-touch


【解决方案1】:

我没有 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"

【讨论】:

  • 这应该是答案。依赖屏幕大小在很多层面都是错误的。
  • @3d0,依赖屏幕大小的哪一级是错误的?
  • 您知道您可以使用以前 iPhone 的尺寸构建一个与 iPhone5 兼容的应用程序(您的应用程序将有黑色边框)。如果使用屏幕尺寸,系统会产生应用程序的尺寸(与 iPhone4 尺寸相同)。在模拟器中更糟糕。
  • 你问了一个场景:我有一个应用程序在 iphone5 上运行良好,在 4s 上有点慢,在 4 上非常慢。我可以从屏幕高度分辨 4/4s,但是这不会告诉我它在两者之间是哪个设备。我可以进行速度测试,检查结果,然后据此做出决定,但这会浪费时间。这不是一个好的用户体验。在一次快速检查中,我可以设置参数以使其在每个特定设备上运行得更好。反正这是我的计划。
  • 在我的4S 上,此代码返回iPhone4,1。对于 5,它返回 iPhone5,1。也许两年后模型就变了?
【解决方案2】:

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

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

[ [ UIScreen mainScreen ] bounds ].size.height

当然可以变成宏。

【讨论】:

  • 我不认为你可以使用自动布局来设置 UITableViewCells 的高度,我会再看看以防我错过了什么。
  • 我想你可以。使用 NSLayoutConstraint 以编程方式定义您自己的约束。
  • 您能否使用自动布局并仍然正确处理 iPhone4 和 iPhone5 之间的布局差异?
【解决方案3】:
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)

在某个 constant.h 文件中定义这个宏,并在任何地方使用它来检查

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

【讨论】:

    【解决方案4】:

    从您的问题来看,您似乎想根据设备和分辨率来更正 UI。在这种情况下,这很好,您可以使用下面给定的代码来识别设备。但是对于非 UI 的事情,比如寻找一个设备中存在的设备功能,而另一个设备中不存在,总是建议检查该功能是否存在,而不是设备型号。 来自苹果的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");
      }
    }
    

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 2012-10-23
        • 1970-01-01
        • 2012-11-22
        • 2011-12-28
        • 1970-01-01
        • 2012-04-21
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多