How to get iOS device type at run time like iPhone, iPod or iPad device | Techbirds
Apple provide following way to find out the Device Type or Model such we can make application Universal or can change or load User Interface according to iPad or iPhone5 or iPhone dynamically.
1) Using UIDevice Class
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
//iPad
}else{
if ([UIScreen mainScreen].bounds.size.height > 500) {
//iPhone5 or iPod5
}else{
//iPhone or iPod
}
}
2) Using Predefine Macro UI_USER_INTERFACE_IDIOM()
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//iPad
}else{
if ([UIScreen mainScreen].bounds.size.height > 500) {
//iPhone5 or iPod5
}else{
//iPhone or iPod
}
}
3) Using by creating Macro in pch(Pre Compiled Header Class). Using with macro in pch file we can access in whole application.
#define IS_IPHONE (([[[UIDevice currentDevice] model] rangeOfString:@”iPhone”].location == NSNotFound)?FALSE:TRUE)
#define IS_HEIGHT_GTE_480 (([[UIScreen mainScreen ] bounds].size.height = 500)?TRUE:FALSE)
#define IS_IPHONE_5 (IS_IPHONE && IS_HEIGHT_GTE_568)
#define IS_IPAD_Device (([[[UIDevice currentDevice] model] rangeOfString:@”iPad”].location == NSNotFound)?FALSE:TRUE)
#define IS_IPAD_SIZE (([[UIScreen mainScreen ] bounds].size.height >= 1000)?TRUE:FALSE)
#define IS_IPAD (IS_IPAD_Device && IS_IPAD_SIZE)
We can use above macro as follows in below :-
if (IS_IPHONE) {
NSLog(@”IPHONE”);
}else{
NSLog(@”No IPHONE”);
}
if (IS_HEIGHT_GTE_480) {
NSLog(@”HEIGHT_GTE_480″);
}else{
NSLog(@”No HEIGHT_GTE_480″);
}
if (IS_IPHONE_4) {
NSLog(@”IPHONE_4″);
}else{
NSLog(@”No IPHONE_4″);
}
if (IS_IPHONE_5) {
NSLog(@”IPHONE_5″);
}else{
NSLog(@”No IPHONE_5″);
}
if (IS_IPOD) {
NSLog(@”IPOD”);
}else{
NSLog(@”No IPOD”);
}
if (IS_IPOD_4) {
NSLog(@”IPOD_4″);
}else{
NSLog(@”No IPOD_4″);
}
if (IS_IPOD_5) {
NSLog(@”IPOD_5″);
}else{
NSLog(@”No IPOD_5″);
}
if (IS_HEIGHT_GTE_568) {
NSLog(@”HEIGHT_GTE_568″);
}else{
NSLog(@”No HEIGHT_GTE_568″);
}
if (IS_IPAD_Device) {
NSLog(@”IPAD_Device”);
}else{
NSLog(@”No IPAD_Device”);
}
if (IS_IPAD_SIZE) {
NSLog(@”IPAD_SIZE”);
}else{
NSLog(@”No IPAD_SIZE”);
}
if (IS_IPAD) {
NSLog(@”IPAD”);
}else{
NSLog(@”No IPAD”);
}
871 total views, 1 views today
Share this On Tags: device type, ipad, iphone, iphone5, ipod