gpt4 book ai didi

ios - 即使在 iPhone 5 上运行 IS IPHONE_5 也会返回 0

转载 作者:行者123 更新时间:2023-12-01 17:30:54 25 4
gpt4 key购买 nike

我已经使用此代码来检测设备是否为 iPhone 5 以设置适当的 UI。

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

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

最佳答案

嗯,一开始你也确实把 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

为确保您可以在每个文件中使用此宏,请执行以下步骤:
创建一个新文件(让我们称之为“Helperfunctions”)

.h 文件包含函数的定义,仅此而已:
NSString* deviceName();

.m 文件包含给定的设备名称代码:
#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

关于ios - 即使在 iPhone 5 上运行 IS IPHONE_5 也会返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22980213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com