iPad 2检测

Mar*_*tin 6 objective-c ipad ios ipad-2

由于我没有iPad 2,因此在调用[[UIDevice currentDevice] model]时我需要知道它返回的内容.我认为它只返回"iPad",但似乎我错了.

有人可以告诉我吗?

谢谢

Mar*_*tos 27

检查带相机的iPad.

BOOL isIPad2 = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad &&
                [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]);
Run Code Online (Sandbox Code Playgroud)

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

  • @oberthelot:这是可以想象的,但是iPad的未来版本似乎不太可能排除相机.因此,除非OP想要排除以后的模型,否则这将正常工作.我猜这个变量可以重命名为`isIPad2OrLater`以使其更清晰 - 并且将来更有可能保持真实. (3认同)

Tom*_*ana 13

model除了出于信息目的或诊断输出而显示它之外,切勿使用该属性.它不能保证保留,如果你依赖它,你会不必要地切断新设备.

许多iPhone应用程序无法在iPad的兼容模式下使用,因为他们检查了model属性,如果不是iPhone/iPod,他们没有做任何事情.

  • 不要检测iPad 2,检测iPad 2的功能.很可能iPad 3和其他设备也可以使用它们. (2认同)

Dan*_*kin 9

要获得精确的模型字符串,例如"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
Run Code Online (Sandbox Code Playgroud)


bri*_*ear 7

UIScreen + Retina.h #import

@interface UIScreen(Retina)

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


@end
Run Code Online (Sandbox Code Playgroud)

UIScreen + Retina.m

#import "UIScreen+Retina.h"

@implementation UIScreen(Retina)

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

@end
Run Code Online (Sandbox Code Playgroud)

用法

#import "UIScreen+Retina.h"
Run Code Online (Sandbox Code Playgroud)

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

if ([[UIScreen mainScreen] isRetina]) {
    // Retina display
}
Run Code Online (Sandbox Code Playgroud)

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;

    }
}
Run Code Online (Sandbox Code Playgroud)