Pages

Tuesday, 6 January 2015

How to detect iOS device model by macro



Steps 1]

Define macro's as follows in .pch file or create new header file (like in example Header.h )

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))

#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)

#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)




Steps 2] use following conditions (to check isRetina Supports or Not / is iPAD or iPhone / which iPhone device model used / Max Screen Length )


 if(IS_IPAD)
    {
        NSLog(@"IT'S IPAD");
    }
    if(IS_IPHONE)
    {
        NSLog(@"IT'S IPHONE");
    }
    
    //retina support
    if(IS_RETINA)
    {
        NSLog(@"RETINA SUPPORTED");
    }
    else
    {
        NSLog(@"RETINA NOT SUPPORTED");
    }
    
    
    
    
    if(IS_IPHONE_4_OR_LESS)
    {
        NSLog(@"IT'S  IPHONE_4_OR_LESS");
    }
    if(IS_IPHONE_5)
    {
        NSLog(@"IT'S IPHONE_5 or 5 IPHONE_5s ");
    }
    if(IS_IPHONE_6)
    {
        NSLog(@"IT'S IPHONE_6");
    }
    if(IS_IPHONE_6P)
    {
        NSLog(@"IT'S IPHONE_6Plus");
    }
    
    NSLog(@"================================================");
    NSLog(@"SCREEN_WIDTH: %.2f", SCREEN_WIDTH);
    NSLog(@"SCREEN_HEIGHT: %.2f", SCREEN_HEIGHT);
    NSLog(@"SCREEN_MAX_LENGTH: %.2f", SCREEN_MAX_LENGTH);
    NSLog(@"SCREEN_MIN_LENGTH: %.2f", SCREEN_MIN_LENGTH);



No comments:

Post a Comment