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

fuz*_*oat 7 iphone cocoa-touch objective-c ios

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

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

edz*_*o27 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]];
}
Run Code Online (Sandbox Code Playgroud)

记得导入:

#import "sys/utsname.h"
Run Code Online (Sandbox Code Playgroud)

  • 这应该是答案.在如此多的级别中依赖于屏幕尺寸是错误的. (2认同)

小智 8

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

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

[ [ UIScreen mainScreen ] bounds ].size.height
Run Code Online (Sandbox Code Playgroud)

当然可以变成一个宏观.