How can i get iphone device type?

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 so, please tell me.

+6
source share
8 answers

Caleb is correct, you should not check the device type, but for functionality. For example, you can check if the device supports multitasking:

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 need, you can check the iOS version, but know that this is not a replacement for checking the respondsToSelector: object.

 #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.

+8
source

I found a new algorithm for getting 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"; } 
+15
source

In fact, the UIDevice class UIDevice not have a platformString method. With undocumented methods, your application will be rejected by Apple.

 [[UIDevice currentDevice] model] // eg "iPod touch" 

will do the trick.

+9
source

I added a static method for my project to check the type of device: iPad, iPhone4 (or less) and iPhone5 to handle 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 UIUserInterfaceIdiomPad type.

From what I can say, iPod and iPhone should be treated the same. But if you need to determine if this is an iPod, check to see if it makes phone calls.

+4
source

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

 [[UIDevice currentDevice] platformString] //@"iPhone 4" 
+2
source

This post helped me distinguish between the types of iPhone / iPad:

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

 [[UIDevice currentDevice] model] 

And for further refinement, you can import and find the specific type of model returned as a char array with the following code:

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

And convert it to NSString with:

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

The usual advice is not to worry about which device you are working on, but instead check the functions you need. If you look at the type of device, you must make the assumptions wrong. The options for each model sold in different markets may differ, for example, and new devices may come from this report of the same type, but have different functions. So, check the properties, not the model.

+1
source

You can define some macros that extend from @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)
#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) 
+1
source

Source: https://habr.com/ru/post/898439/


All Articles