IS IPHONE 5即使在iPhone 5上运行也会返回0

时间:2014-04-10 06:30:23

标签: ios iphone iphone-5 uiscreen

我已使用此代码检测设备是否为iPhone 5,以设置适当的用户界面。

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

但是,即使应用程序在iPhone 5c中运行,它也会返回0。 我还提到了How to detect iPhone 5 (widescreen devices)?但仍然更新的代码不起作用。请帮忙。

3 个答案:

答案 0 :(得分:4)

嗯,首先你也认为iPhone5S是iPhone 5,这是不一样的。 我宁愿使用设备名称,因为您的宏可以根据需要进行扩展。

#import <sys/utsname.h>
NSString* deviceName()
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *result = [NSString stringWithCString:systemInfo.machine
                                          encoding:NSUTF8StringEncoding];
    return result;
}

#define isIPhone5  [deviceName() rangeOfString:@"iPhone5,"].location != NSNotFound
#define isIPhone5S [deviceName() rangeOfString:@"iPhone6,"].location != NSNotFound

设备列表在这里:

/*
 @"i386"      on the simulator
 @"iPod1,1"   on iPod Touch
 @"iPod2,1"   on iPod Touch Second Generation
 @"iPod3,1"   on iPod Touch Third Generation
 @"iPod4,1"   on iPod Touch Fourth Generation
 @"iPod5,1"   on iPod Touch Fifth Generation
 @"iPhone1,1" on iPhone
 @"iPhone1,2" on iPhone 3G
 @"iPhone2,1" on iPhone 3GS
 @"iPad1,1"   on iPad
 @"iPad2,1"   on iPad 2
 @"iPad3,1"   on 3rd Generation iPad
 @"iPad3,2":  on iPad 3(GSM+CDMA)
 @"iPad3,3":  on iPad 3(GSM)
 @"iPad3,4":  on iPad 4(WiFi)
 @"iPad3,5":  on iPad 4(GSM)
 @"iPad3,6":  on iPad 4(GSM+CDMA)
 @"iPhone3,1" on iPhone 4
 @"iPhone4,1" on iPhone 4S
 @"iPhone5,1" on iPhone 5
 @"iPad3,4"   on 4th Generation iPad
 @"iPad2,5"   on iPad Mini
 @"iPhone5,1" on iPhone 5(GSM)
 @"iPhone5,2" on iPhone 5(GSM+CDMA)
 @"iPhone5,3  on iPhone 5c(GSM)
 @"iPhone5,4" on iPhone 5c(GSM+CDMA)
 @"iPhone6,1" on iPhone 5s(GSM)
 @"iPhone6,2" on iPhone 5s(GSM+CDMA)
 */

我的方法与此处的帖子有关,但可以在前缀标题中轻松使用: Detect if the device is iPhone 5s


要确保您可以在每个文件中使用此宏,请按照下列步骤操作: 创建一个新文件(让我们称之为&#34; Helperfunctions&#34;)

.h文件包含函数的定义,没有别的:

NSString* deviceName();

.m文件包含给定的deviceName-code:

#import "Helperfunctions.h"
#import <sys/utsname.h>

NSString* deviceName()
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *result = [NSString stringWithCString:systemInfo.machine
                                          encoding:NSUTF8StringEncoding];
    return result;
}

在预编译的头文件中,您现在可以添加代码(或者已经在.pch中导入的另一个文件,所以它看起来像:

//
//  Prefix header
//
//  The contents of this file are implicitly included at the beginning of every source file.
//

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>

    #import "Helperfunctions.h"

    #define isIPhone5  [deviceName() rangeOfString:@"iPhone5,"].location != NSNotFound
    #define isIPhone5S [deviceName() rangeOfString:@"iPhone6,"].location != NSNotFound
#endif

答案 1 :(得分:2)

或者,如果你想要类似于你已经做过的事情,为什么不呢:

BOOL iPhone5 = [[UIScreen mainScreen] bounds] .size.height&gt; = 568;

答案 2 :(得分:1)

您的应用是否有R4启动图像(如果您使用的是资产目录)或Default-568h@2x.png启动图像?如果没有,它会认为它正在3.5英寸屏幕上运行,无论它实际运行的是什么设备。