How to check iPhone version in iOS?

Hi, I want to check the version of the iPhone device in iOS.

I mean, the iPhone 4 or iPhone 5 is currently running.

Do I need to check the device, is it iPhone 5 or not?

Because I have some problems in my application that the iPhone 5 should know or not.

So how can I?

+4
source share
5 answers

Add this code:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){ if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) { CGSize result = [[UIScreen mainScreen] bounds].size; CGFloat scale = [UIScreen mainScreen].scale; result = CGSizeMake(result.width * scale, result.height * scale); if(result.height == 960) { NSLog(@"iPhone 4 Resolution"); resolution_number = 1; } if(result.height == 1136) { NSLog(@"iPhone 5 Resolution"); } } else{ NSLog(@"Standard Resolution"); } } 
+9
source

Add these macros to your code:

 #define HEIGHT_IPHONE_5 568 #define IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) #define IS_IPHONE_5 ([[UIScreen mainScreen] bounds ].size.height == HEIGHT_IPHONE_5 ) 

then just check when you need to.

 if (IS_IPHONE_5) { //Code for iPhone5 }else{ //Code for earlier version } 
+9
source

Based on the answers to this question, you can determine which model you are using thereby:

 NSString *deviceModel = [UIDevice currentDevice].model; 

The results can be one of the values: iPod touch, iPhone, iPad, iPhone Simulator, iPad Simulator

For a particular model, which version you are using can be determined by creating the Category on UIDevice.h as follows:

UIDevice + Utilities.h

 #import <UIKit/UIKit.h> @interface UIDevice (Utilities) - (CGFloat)deviceModelVersion; @end 

UIDevice + Utilities.m

 #import "UIDevice+Utilities.h" #include <sys/types.h> #include <sys/sysctl.h> @implementation UIDevice (Utilities) - (CGFloat)deviceModelVersion { size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = malloc(size); sysctlbyname("hw.machine", machine, &size, NULL, 0); NSString *platform = [NSString stringWithUTF8String:machine]; free(machine); if ([platform rangeOfString:@"iPhone"].location != NSNotFound) { return [[[platform stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue]; } else if ([platform rangeOfString:@"iPad"].location != NSNotFound) { return [[[platform stringByReplacingOccurrencesOfString:@"iPad" withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue]; } else if ([platform rangeOfString:@"iPod"].location != NSNotFound) { return [[[platform stringByReplacingOccurrencesOfString:@"iPod" withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue]; } else if ([platform rangeOfString:@"i386"].location != NSNotFound || [platform rangeOfString:@"x86_64"].location != NSNotFound) { return -1.0; //Currently it is not possible (or maybe it is, but I do not know) //which type of simulator device model version your app is running //so I am returning -1.0 device model version for all simulators types } return 0.0; } @end 

An example of calling the deviceModelVersion function:

 CGFloat deviceModelVersion = [UIDevice currentDevice].deviceModelVersion; 

Possible results can be 1.0, 1.1, ..., 2.0, 2.1, ..., 3.0, 3.1, .., 4.0, 4.1, .., 5.0, 5.1, ...

To determine if this is an iPhone 5, you will have

 deviceModel : "iPhone" 

and

 deviceModelVersion : >=5.0 and < 6.0 
+5
source

I really found a #define that does the trick

 #define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double) 568) < DBL_EPSILON) 
+1
source
 #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0f) #define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f) #define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f) #define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f) #define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f) #define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0f) 
0
source

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


All Articles