iPad 2 检测

发布于 2024-10-24 03:30:34 字数 120 浏览 5 评论 0原文

由于我没有 iPad 2,因此我需要知道调用 [[UIDevice currentDevice] model] 时它返回什么。我以为它只返回“iPad”,但看来我错了。

有人可以告诉我吗?

谢谢

Since I don't have iPad 2, I need to know what it returns when calling [[UIDevice currentDevice] model]. I thought it returns just "iPad" but it seems I'm wrong.

Can somebody let me know?

Thanks

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

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

发布评论

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

评论(11

撧情箌佬 2024-10-31 03:30:35

首先我应该提到,我花了很多时间才弄清楚为什么 ipad 模拟器“说”它是 iphone。对我来说,事实证明我只需将其切换为通用:

在此处输入图像描述

这是我认为的代码是相当典型的检测代码。还有其他一些可能也有效,但是......

// lifted this from the ios 4 cookbook:
- (BOOL) isiPad{

    BOOL result = NO;

    NSString *classAsString = 
    NSStringFromClass([UISplitViewController class]);

    if (classAsString == nil ||
        [classAsString length] == 0){
        return(NO);
    }

    UIDevice *device = [UIDevice currentDevice];

    if ([device respondsToSelector:
         @selector(userInterfaceIdiom)] == NO){
        return(NO);
    }

    NSLog(@"Device: %d", [[UIDevice currentDevice] userInterfaceIdiom]);
    NSLog(@"Device: %@", [[UIDevice currentDevice] model]);

    if ([device userInterfaceIdiom] != UIUserInterfaceIdiomPad){
        return(NO);
    }

    // you can put some screen size tests here too if you'd like
    result = YES;

    return(result);
}

First off I should mention that it took me a great deal of time to figure out why the ipad simulator was "saying" it was iphone. For me, it turned out I just had to switch it over to universal:

enter image description here

Here's the code which I think is fairly typical detection code for this. There are others that probably work too but...

// lifted this from the ios 4 cookbook:
- (BOOL) isiPad{

    BOOL result = NO;

    NSString *classAsString = 
    NSStringFromClass([UISplitViewController class]);

    if (classAsString == nil ||
        [classAsString length] == 0){
        return(NO);
    }

    UIDevice *device = [UIDevice currentDevice];

    if ([device respondsToSelector:
         @selector(userInterfaceIdiom)] == NO){
        return(NO);
    }

    NSLog(@"Device: %d", [[UIDevice currentDevice] userInterfaceIdiom]);
    NSLog(@"Device: %@", [[UIDevice currentDevice] model]);

    if ([device userInterfaceIdiom] != UIUserInterfaceIdiomPad){
        return(NO);
    }

    // you can put some screen size tests here too if you'd like
    result = YES;

    return(result);
}
后来的我们 2024-10-31 03:30:35

您可以从以下网站获取所有iPad2退货型号或您想要的iOS设备型号:
IOS 设备型号和平台

您也可以使用以下代码直接检索型号:

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

- (NSString *) platformString
{
    NSString *platform = [self deviceModel];
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone_2G";
    else if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone_3G";
    else if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone_3GS";
    else if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone_4";
    else if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon_iPhone_4";
    else if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone_4S";
    else if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone_5";
    else if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone_5";
    else if ([platform isEqualToString:@"iPod1,1"])      return @"iPod_Touch 1G";
    else if ([platform isEqualToString:@"iPod2,1"])      return @"iPod_Touch 2G";
    else if ([platform isEqualToString:@"iPod3,1"])      return @"iPod_Touch 3G";
    else if ([platform isEqualToString:@"iPod4,1"])      return @"iPod_Touch 4G";
    else if ([platform isEqualToString:@"iPad1,1"])           return @"iPad_1G";
    else if ([platform isEqualToString:@"iPad2,1"])      return @"iPad_2(WiFi)";
    else if ([platform isEqualToString:@"iPad2,2"])      return @"iPad_2(GSM)";
    else if ([platform isEqualToString:@"iPad2,3"])      return @"iPad_2(CDMA)";
    else if ([platform isEqualToString:@"iPad3,1"])      return @"iPad_3";
    else if ([platform isEqualToString:@"iPad3,2"])      return @"iPad_3(GSM/CDMA)";
    else if ([platform isEqualToString:@"iPad3,3"])      return @"iPad_3(GSM)";
    else if ([platform isEqualToString:@"iPad3,4"])      return @"iPad_3(GSM)";
    else if ([platform isEqualToString:@"iPad2,5"])      return @"iPad_mini_1G";
    else if ([platform isEqualToString:@"i386"])         return @"Simulator";
    else if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}

You can get all IPad2 return model or the model of the iOS device that you want from the following website:
IOS Devices Models and Platforms

Also you can use the following code to retrieve the model direct:

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

- (NSString *) platformString
{
    NSString *platform = [self deviceModel];
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone_2G";
    else if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone_3G";
    else if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone_3GS";
    else if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone_4";
    else if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon_iPhone_4";
    else if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone_4S";
    else if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone_5";
    else if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone_5";
    else if ([platform isEqualToString:@"iPod1,1"])      return @"iPod_Touch 1G";
    else if ([platform isEqualToString:@"iPod2,1"])      return @"iPod_Touch 2G";
    else if ([platform isEqualToString:@"iPod3,1"])      return @"iPod_Touch 3G";
    else if ([platform isEqualToString:@"iPod4,1"])      return @"iPod_Touch 4G";
    else if ([platform isEqualToString:@"iPad1,1"])           return @"iPad_1G";
    else if ([platform isEqualToString:@"iPad2,1"])      return @"iPad_2(WiFi)";
    else if ([platform isEqualToString:@"iPad2,2"])      return @"iPad_2(GSM)";
    else if ([platform isEqualToString:@"iPad2,3"])      return @"iPad_2(CDMA)";
    else if ([platform isEqualToString:@"iPad3,1"])      return @"iPad_3";
    else if ([platform isEqualToString:@"iPad3,2"])      return @"iPad_3(GSM/CDMA)";
    else if ([platform isEqualToString:@"iPad3,3"])      return @"iPad_3(GSM)";
    else if ([platform isEqualToString:@"iPad3,4"])      return @"iPad_3(GSM)";
    else if ([platform isEqualToString:@"iPad2,5"])      return @"iPad_mini_1G";
    else if ([platform isEqualToString:@"i386"])         return @"Simulator";
    else if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}
驱逐舰岛风号 2024-10-31 03:30:35

您在这里得到了很多答案,其中阐述了为什么您不应该这样做的意见,并为您提供了替代代码,但您没有得到问题的任何实际答案。如果您想准确确定您正在哪个设备上运行(无论您想要什么目的......不要让其他开发人员假装知道您想要完成什么),您可以使用 < code>UIDeviceHardware 第三方类。您可以在这里找到它:

https://github.com/fahrulazmi/UIDeviceHardware

您只需调用:

NSString *platformString = [UIDeviceHardware platformString];

并且它将返回设备。在您的情况下,您需要将 platformString 与以下任何一个相匹配:

@"iPad 2 (WiFi)"
@"iPad 2 (GSM)"
@"iPad 2 (CDMA)"

You are getting a lot of answers on here that state opinions on why you shouldn't be doing this and are giving you alternative code, but you aren't getting any actual answers to your question. If you want to determine exactly which device you are running on (for whatever purpose you want... don't let other developers pretend to know what you are trying to accomplish), you can using the UIDeviceHardware third-party class. You can find it here:

https://github.com/fahrulazmi/UIDeviceHardware

You'll simply call:

NSString *platformString = [UIDeviceHardware platformString];

And it will return the device. In your case, you'd be looking to match the platformString to any of these:

@"iPad 2 (WiFi)"
@"iPad 2 (GSM)"
@"iPad 2 (CDMA)"
吻安 2024-10-31 03:30:34

检查是否有带摄像头的 iPad。

BOOL isIPad2 = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad &&
                [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]);

请注意,通常最好检测特定功能,而不是基于模型/版本检测进行全面假设。例如,如果您需要相机,则明确测试相机;如果您需要根据可用 RAM 量调整 UI 质量,测试物理 RAM;等。另请注意我写的评论强调了使用的危险模型检测。

Check for an iPad with a camera.

BOOL isIPad2 = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad &&
                [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]);

Note that it is generally better to detect specific features rather than make blanket assumptions based on model/version detection. For instance, if you need a camera, then test for the camera explicitly; if you need to tweak the UI quality based on the amount of RAM available, test for physical RAM; etc. Also note a comment I wrote that highlights the dangers of using model detection.

屋檐 2024-10-31 03:30:34

切勿model 属性用于除显示信息或诊断输出之外的任何其他用途。它不能保证被保留,如果您依赖它,您就不必要地在新设备到来时切断它们。

许多 iPhone 应用程序无法在 iPad 的兼容模式下使用,只是因为它们检查了 model 属性,如果不是 iPhone / iPod,它们就不会执行任何操作。

Never use the model property for anything else than displaying it for informational purposes or diagnostics output. It is not guaranteed to be preserved and if you rely on it, you unnecessarily cut off new devices as they come.

Lots of iPhone apps could not be used in the compatibility mode of iPad just because they checked the model property and if it wasn't iPhone / iPod they didn't do anything.

红衣飘飘貌似仙 2024-10-31 03:30:34

要获得精确的型号字符串,例如“iPad2,2”,您可以使用类似的字符串。

#import "YourDeviceDetectionClass.h"
#include <sys/utsname.h>

@implementation YourDeviceDetectionClass

+(NSString*)modelAsString
{
    struct utsname platform;
    int rc = uname(&platform);
    if(rc == -1)
    {
        // Error...
        return nil;
    }
    else
    {
        // Convert C-string to NSString
        return [NSString stringWithCString:platform.machine encoding:NSUTF8StringEncoding];
    }
}

@end

To get the precise model string, e.g. "iPad2,2", you could use something like this.

#import "YourDeviceDetectionClass.h"
#include <sys/utsname.h>

@implementation YourDeviceDetectionClass

+(NSString*)modelAsString
{
    struct utsname platform;
    int rc = uname(&platform);
    if(rc == -1)
    {
        // Error...
        return nil;
    }
    else
    {
        // Convert C-string to NSString
        return [NSString stringWithCString:platform.machine encoding:NSUTF8StringEncoding];
    }
}

@end
与酒说心事 2024-10-31 03:30:34

UIScreen+Retina.h
#import

@interface UIScreen(Retina)

// Returns YES if this is a Retina display.
- (BOOL)isRetina;


@end

UIScreen+Retina.m

#import "UIScreen+Retina.h"

@implementation UIScreen(Retina)

- (BOOL)isRetina {
    return [self respondsToSelector:@selector(displayLinkWithTarget:selector:)] && (self.scale == 2.0);
}

@end

用法

#import "UIScreen+Retina.h"

//http://stackoverflow.com/questions/3294100/how-to-

if ([[UIScreen mainScreen] isRetina]) {
    // Retina display
}

Differentiate- Between-iphone4-and-iphone-3 IPAD/IPHONE 高分辨率/低分辨率

#import "UIScreen+Retina.h"
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
    //IPAD        
    if ([[UIScreen mainScreen] isRetina]) {
        // IPAD 3 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_HIGHRES;            
    }else{
        //iPAD 1/2
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_LOWRES;        }
}else{
    //IPHONE
    if ([[UIScreen mainScreen] isRetina]) {
        // IPHONE 4/4s/5 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_HIGHRES;

    }else{
        //IPHONE (3.x)
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_LOWRES;

    }
}

UIScreen+Retina.h
#import

@interface UIScreen(Retina)

// Returns YES if this is a Retina display.
- (BOOL)isRetina;


@end

UIScreen+Retina.m

#import "UIScreen+Retina.h"

@implementation UIScreen(Retina)

- (BOOL)isRetina {
    return [self respondsToSelector:@selector(displayLinkWithTarget:selector:)] && (self.scale == 2.0);
}

@end

USAGE

#import "UIScreen+Retina.h"

//http://stackoverflow.com/questions/3294100/how-to-differentiate-between-iphone4-and-iphone-3

if ([[UIScreen mainScreen] isRetina]) {
    // Retina display
}

IPAD/IPHONE HIGH/LOW RES

#import "UIScreen+Retina.h"
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
    //IPAD        
    if ([[UIScreen mainScreen] isRetina]) {
        // IPAD 3 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_HIGHRES;            
    }else{
        //iPAD 1/2
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_LOWRES;        }
}else{
    //IPHONE
    if ([[UIScreen mainScreen] isRetina]) {
        // IPHONE 4/4s/5 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_HIGHRES;

    }else{
        //IPHONE (3.x)
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_LOWRES;

    }
}
梦冥 2024-10-31 03:30:34

我想你可以测试一下规模

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[[UIScreen mainScreen] scale] == 2.0) {
        // retina display
    } 
}

I think you can test it scale

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[[UIScreen mainScreen] scale] == 2.0) {
        // retina display
    } 
}
秋意浓 2024-10-31 03:30:34

如果您使用 OpenGL,您可以查看 GPU 模型。 iPad 1 配备 PowerVR SGX 535,iPad 2 配备 PowerVR SGX 543。

const char * deviceStr = (const char *)glGetString(GL_RENDERER);
if (!strcmp(deviceStr, "PowerVR SGX 535")) {
    // iPad 1
}
else {
    // iPad 2 or later
}

If you're using OpenGL you can look at the GPU model. iPad 1 has a PowerVR SGX 535 and iPad 2 has a PowerVR SGX 543.

const char * deviceStr = (const char *)glGetString(GL_RENDERER);
if (!strcmp(deviceStr, "PowerVR SGX 535")) {
    // iPad 1
}
else {
    // iPad 2 or later
}
逆流 2024-10-31 03:30:34

UIDevice 类参考 应该有所帮助。如需更具体的解决方案,请尝试这个SO问题

至于你的第二个问题,测试分辨率的最佳方法是获取与iPad3,1的PPI数相似的显示器。不幸的是,您可能无法做到。测试任何应用程序的最佳方法是在实际设备上。

UIDevice Class Reference should help. For more specific solutions, try this SO question.

As for your second question, the best way to test the resolution would be to acquire a display similar to the number of PPI of the iPad3,1. Unfortunately, you probably won't be able to. The best way to test any app is on the actual device.

找回味觉 2024-10-31 03:30:34

此行将打印设备版本:

Ipad 1: Platform: iPad1,1

Ipad 2: Platform: iPad2,1

Ipad 3: Platform: iPad3,3

Iphone 4S: Platform: iPhone4,1

Simulator: Platform: x86_64

size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *answer = (char*)malloc(size);
sysctlbyname("hw.machine", answer, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
free(answer);
NSLog(@"Platform: %@", platform);

This lines will print the device version:

Ipad 1: Platform: iPad1,1

Ipad 2: Platform: iPad2,1

Ipad 3: Platform: iPad3,3

Iphone 4S: Platform: iPhone4,1

Simulator: Platform: x86_64

size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *answer = (char*)malloc(size);
sysctlbyname("hw.machine", answer, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
free(answer);
NSLog(@"Platform: %@", platform);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文