如何获取 iPhone 设备类型?

发布于 2024-12-07 19:54:04 字数 100 浏览 1 评论 0原文

我想获取 iphone 设备类型,如果它是 xcode 4.0 中的 iphone 2 或 3G 或 4。

有什么办法可以得到吗?

如果是的话,请告诉我。

I want to get iphone device type if it is iphone 2 or 3G or 4 in xcode 4.0.

Is there any way to get it?

If it is, please tell me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

赴月观长安 2024-12-14 19:54:04

我发现了一些新的算法来获取设备类型和 iOS 版本。

#include <sys/sysctl.h>
#include <sys/utsname.h>


- (NSString *) platformString{

NSLog(@"[UIDevice currentDevice].model: %@",[UIDevice currentDevice].model);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);
NSLog(@"[UIDevice currentDevice].batteryLevel: %f",[UIDevice currentDevice].batteryLevel);      
struct utsname systemInfo;
uname(&systemInfo);
NSLog(@"[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]: %@",[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4 CDMA";
if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (Cellular)";
if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (Cellular)";
if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (Cellular)";
if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (Cellular)";
if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (Cellular)";
if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (Cellular)";
if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (Cellular)";
if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (Cellular)";
if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
if ([platform isEqualToString:@"i386"])         return @"Simulator";
if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
return @"Unknown";
}

I found some new algorithm to get device type and iOS version.

#include <sys/sysctl.h>
#include <sys/utsname.h>


- (NSString *) platformString{

NSLog(@"[UIDevice currentDevice].model: %@",[UIDevice currentDevice].model);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);
NSLog(@"[UIDevice currentDevice].batteryLevel: %f",[UIDevice currentDevice].batteryLevel);      
struct utsname systemInfo;
uname(&systemInfo);
NSLog(@"[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]: %@",[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,2"])    return @"iPhone 4 CDMA";
if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
if ([platform isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
if ([platform isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (Cellular)";
if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (Cellular)";
if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (Cellular)";
if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (Cellular)";
if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (Cellular)";
if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (Cellular)";
if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (Cellular)";
if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (Cellular)";
if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
if ([platform isEqualToString:@"i386"])         return @"Simulator";
if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
return @"Unknown";
}
断桥再见 2024-12-14 19:54:04

实际上,UIDevice 类没有方法platformString。如果使用未记录的方法,您的应用程序将被 Apple 拒绝。

[[UIDevice currentDevice] model] // e.g. "iPod touch"

会成功的。

Actually, the UIDevice class does not have a method platformString. With undocumented methods your app will get rejected by Apple.

[[UIDevice currentDevice] model] // e.g. "iPod touch"

will do the trick.

伤感在游骋 2024-12-14 19:54:04

Caleb 是对的,您不应该检查设备类型,而应该检查功能。例如,您可以检查设备是否支持多任务,如下所示:

UIDevice *device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
    // ...code to be executed if multitasking is supported.
    // respondsToSelector: is very useful
}

如果必须,您可以检查 iOS 版本,但要知道这不能替代检查对象是否 respondsToSelector:

#define IOS4_0 40000

// You'd probably want to put this in a convenient method
NSArray *versions = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
NSInteger major = [[versions objectAtIndex:0] intValue];
NSInteger minor = [[versions objectAtIndex:1] intValue];
NSInteger version = major * 10000 + minor * 100;

if (version >= IOS4_0) {
    // ...code to be executed for iOS4.0+
}

据我所知,没有记录的方法来检查设备型号。

Caleb is right, you shouldn't check for device type, but for functionality. For example, you can check whether the device supports multitasking like so:

UIDevice *device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
    // ...code to be executed if multitasking is supported.
    // respondsToSelector: is very useful
}

If you must, you can check the iOS version, but know this is not a substitute for checking whether an object respondsToSelector:.

#define IOS4_0 40000

// You'd probably want to put this in a convenient method
NSArray *versions = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
NSInteger major = [[versions objectAtIndex:0] intValue];
NSInteger minor = [[versions objectAtIndex:1] intValue];
NSInteger version = major * 10000 + minor * 100;

if (version >= IOS4_0) {
    // ...code to be executed for iOS4.0+
}

As far as I know, there is no documented way to check the device model.

很酷又爱笑 2024-12-14 19:54:04

我在我的项目中添加了一个静态方法来检查设备类型:iPad、iPhone4(或更低)和iPhone5,以处理不同的屏幕尺寸。

+ (DeviceType) currentDeviceType
{
    DeviceType device = DEVICE_TYPE_IPAD ;
    if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
    {
        if( [[UIScreen mainScreen] bounds].size.height >= 568 || [[UIScreen mainScreen] bounds].size.width >= 568 )
        {
            device = DEVICE_TYPE_IPHONE5 ;
        }
        else
        {
            device = DEVICE_TYPE_IPHONE4 ;
        }
    }

    return device ;
}

您还可以使用 UIUserInterfaceIdiomPad 类型。

据我所知,iPod 和 iPhone 的目的是平等对待。但如果您需要确定它是否是 iPod,请检查它是否可以打电话。

I have added a static method to my project to check the device type: iPad, iPhone4(or less), and iPhone5 to handle the different screen sizes.

+ (DeviceType) currentDeviceType
{
    DeviceType device = DEVICE_TYPE_IPAD ;
    if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
    {
        if( [[UIScreen mainScreen] bounds].size.height >= 568 || [[UIScreen mainScreen] bounds].size.width >= 568 )
        {
            device = DEVICE_TYPE_IPHONE5 ;
        }
        else
        {
            device = DEVICE_TYPE_IPHONE4 ;
        }
    }

    return device ;
}

You can also use the type UIUserInterfaceIdiomPad.

From what I can tell, the iPod and iPhone are intended to be treated equally. But if you need to determine if it is an iPod, check to see if it makes phone calls.

千年*琉璃梦 2024-12-14 19:54:04

看看这个 https://github.com/erica/uidevice-extension/

[[UIDevice currentDevice] platformString] //@"iPhone 4"

Check this out https://github.com/erica/uidevice-extension/

[[UIDevice currentDevice] platformString] //@"iPhone 4"
情泪▽动烟 2024-12-14 19:54:04

这篇文章帮助我区分了 iPhone/iPad 类型:

您可以获取型号type (iPhone, iPad, iPod) 如上所示:

[[UIDevice currentDevice] model]

为了进一步指定,您可以导入并查找通过以下代码返回为 char 数组的特定模型类型:

struct utsname u;
uname(&u);
char *type = u.machine;

并将其转换为 NSString:

NSString *strType = [NSString stringWithFormat:@"%s", type];

This post helped me to distinguish between iPhone/iPad types:

You can get the model type (iPhone, iPad, iPod) as shown above:

[[UIDevice currentDevice] model]

And to further specify you can import and find the specific model type returned as char array by the following code:

struct utsname u;
uname(&u);
char *type = u.machine;

And convert it to NSString by:

NSString *strType = [NSString stringWithFormat:@"%s", type];
一人独醉 2024-12-14 19:54:04

通常的建议是不要担心您运行的设备类型,而是测试您需要的功能。如果您查看设备类型,您一定会做出不正确的假设。例如,在不同市场销售的每种型号的版本可能会有所不同,并且新设备可能会报告相同的类型但具有不同的功能。因此,测试功能而不是模型。

The usual advice is to not worry about what kind of device you're running on, but instead to test for the features you need. If you look at the device type, you're bound to make assumptions that are incorrect. The versions of each model sold in different markets may vary, for example, and new devices may come along that report the same type but have different features. So, test for features rather than model.

吃兔兔 2024-12-14 19:54:04

您可以定义一些扩展 @csb 对此线程的回答的宏。


#define IS_IPHONE4 ([[UIScreen mainScreen] bounds].size.width == 480 || [[UIScreen mainScreen] bounds].size.height == 480)
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width == 568 || [[UIScreen mainScreen] bounds].size.height == 568)
#define IS_IPAD    ([[UIScreen mainScreen] bounds].size.width == 768 || [[UIScreen mainScreen] bounds].size.height == 768)

You could define some macros extending off of @csb 's answer to this thread.


#define IS_IPHONE4 ([[UIScreen mainScreen] bounds].size.width == 480 || [[UIScreen mainScreen] bounds].size.height == 480)
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width == 568 || [[UIScreen mainScreen] bounds].size.height == 568)
#define IS_IPAD    ([[UIScreen mainScreen] bounds].size.width == 768 || [[UIScreen mainScreen] bounds].size.height == 768)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文