简体   繁体   English

使用宏检测iPhone6和iPhone6 Plus

[英]Detecting iPhone6 & iPhone6 Plus using macros

I am attempting to detect whether the current device is iPhone5, iPhone6 or iPhone 6 Plus. 我试图检测当前的设备是iPhone5,iPhone6还是iPhone 6 Plus。

In my app I am already using this macro to detect iPhone 5 which works perfectly. 在我的应用程序中,我已经使用这个宏来检测完美的iPhone 5。

#define IS_IPHONE_5      (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)

Similarly, I use this macro to detect iPhone 6 and iPhone 6 Plus. 同样,我使用此宏来检测iPhone 6和iPhone 6 Plus。

#define IS_IPHONE_6      (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)667) < DBL_EPSILON)
#define IS_IPHONE_6_PLUS (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)736) < DBL_EPSILON)

The IS_IPHONE_5 macro works as expected in any orientation. IS_IPHONE_5宏以任何方向按预期工作。

My problem is that the IS_IPHONE_6 and IS_IPHONE_6_PLUS macros do not return true when the device is held in LANDSCAPE. 我的问题是当设备保存在LANDSCAPE中时,IS_IPHONE_6和IS_IPHONE_6_PLUS宏不会返回true。 However they do work as expect while the device is held in PORTRAIT. 但是,当设备在PORTRAIT中保存时,它们确实可以正常工作。 What gives? 是什么赋予了?

Also if anyone has a better recommendation to detect iPhone5, 6 and 6 Plus please share. 如果有人有更好的建议来检测iPhone5,6和6 Plus请分享。

This is tested and designed for any combination of iOS system versions and SDK versions 这是针对iOS系统版本和SDK版本的任何组合进行测试和设计的

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))

#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

Note: If iPhone 6 is in zoomed mode the UI is a zoomed up version of iPhone 5. This is reflected in the macros. 注意:如果iPhone 6处于缩放模式,则UI是iPhone 5的放大版本。这反映在宏中。

Usage: http://pastie.org/9687735 用法: http//pastie.org/9687735

Don't use the screen size for this, it's better to use the hardware model. 不要使用屏幕尺寸,最好使用硬件型号。 We are getting more and more screen sizes every year, the less you hard-code screen dimensions in your code the better for your future self. 我们每年都会获得越来越多的屏幕尺寸,您在代码中硬编码屏幕尺寸越少,对您未来的自我感觉就越好。

You need a helper function to get the machine name. 您需要一个辅助函数来获取计算机名称。 I'm using dispatch_once to avoid querying the system multiple times for data that won't change. 我正在使用dispatch_once来避免多次查询系统以获取不会发生变化的数据。

NSString* machineName()
{
    static NSString* name = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        struct utsname systemInfo;
        uname(&systemInfo);
        name = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    });
    return name;
}

Then define a few macros as needed: 然后根据需要定义一些宏:

#define IS_IPHONE_6      [machineName() isEqualToString:@"iPhone7,2"]
#define IS_IPHONE_6_PLUS [machineName() isEqualToString:@"iPhone7,1"]

For some models is trickier: 对于某些型号来说比较棘手:

#define IS_IPHONE_5s     [machineName() hasPrefix:@"iPhone6,"]

Finally, use the macros in your code: 最后,在代码中使用宏:

if (IS_IPHONE_6) {
// for the 6
}

Note: This answer your question (detect models with macros) but you're doing it wrong IMHO. 注意:这回答了你的问题(用宏检测模型),但你做错了恕我直言。 You should use autolayout and size classes, unless you support really old iOS versions... 你应该使用autolayout和size类,除非你支持真正的旧iOS版本......

Take a look at this answer: iOS - How to get device make and model? 看看这个答案: iOS - 如何获得设备制造和型号? it doesn't use macros but it does the job. 它不使用宏,但它完成了这项工作。 I've seen some similar problems with macros to detect iPhone 6 and iPhone 6 plus. 我已经看到一些类似的宏检测iPhone 6和iPhone 6 plus的问题。 So it would be a better idea to try out the answer from @Ohhmee 所以从@Ohhmee那里尝试答案会更好

EDIT: Of course there probably is a solution detecting it with macros. 编辑:当然可能有一个解决方案用宏检测它。 But I don't know that and I can't find a solution so i'm suggesting a different approach. 但我不知道,我找不到解决方案所以我建议采用不同的方法。

Your macros are ridiculous and seem to indicate a fear of floating-point arithmetic. 你的宏很荒谬,似乎表明了对浮点运算的恐惧。

#define IS_IPHONE_5      ([UIScreen mainScreen].bounds.size.height == 568)
#define IS_IPHONE_6      ([UIScreen mainScreen].bounds.size.height == 667)
#define IS_IPHONE_6_PLUS ([UIScreen mainScreen].bounds.size.height == 736)

will work just as good (or just as bad). 将同样好(或同样坏)。 Most likely they will work just as bad. 他们很可能会像坏人一样工作。

If you are interested in features of the iPhone 6 or 6+, check for the features, not the screen size. 如果您对iPhone 6或6+的功能感兴趣,请检查功能,而不是屏幕尺寸。 It is quite likely that Apple will soon have a 4" phone with all the iPhone 6 features. Or a cheap 6c with a big screen but without iPhone 6 features. 苹果很快就会推出一款具有所有iPhone 6功能的4英寸手机。或者是具有大屏幕但没有iPhone 6功能的廉价6c手机。

For user interface and so on, just take the mainScreen bounds and lay out your views accordingly. 对于用户界面等,只需获取主屏幕边界并相应地布置视图。 At this point, hard coding for fixed sizes is absolutely ridiculous and will bite you. 在这一点上,固定大小的硬编码绝对是荒谬的,会咬你。 You should be able to run for example on a 12" iPad with a split screen without problems, and heavens knows what screen size that will be. 你应该能够在没有问题的12英寸iPad屏幕上运行,并且天知道将会是什么屏幕尺寸。

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

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