Identify iPhone 3, 4, and 5 in the same #define

I use the following line in my constants to distinguish between devices and return the device number. How can I identify the iPhone 5 and save it in a single line format?

#define iPhoneType [[UIScreen mainScreen] scale]==2 || [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad ? @"4" : @"3" 

thanks

Edit: A lot of good answers, but my goal is to save it in a single line format for all devices.

Edit: Based on comments, this question needs some clarification. Here are the requirements:

  • A single-line macro that returns either @"3" , @"4" , or @"5" depending on the iOS device.
  • 4-inch devices (currently iPhone 5 and 5th generation iPod touch) should return @"5" .
  • All iPads and all remaining mesh iPhones and iPod touch should return @"4" .
  • All remaining non-retinas iPhone and iPod touch should return @"3" .
+6
source share
5 answers

Assuming the updated requirements are correct, the following should work:

 #define iPhoneType (fabs((double)[UIScreen mainScreen].bounds.size.height - (double)568) < DBL_EPSILON) ? @"5" : ([UIScreen mainScreen].scale==2 || UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? @"4" : @"3") 

This will return @"5" for the 4-inch iPhone and iPod screens. This will return @"4" for all iPads and iPhone and iPod touch. And he will return @"3" for the non-retina iPhone and iPod touch.

+2
source

According to your question, I assume that you want to identify a hardware device, not an iOS version.

 /* Erica Sadun, http://ericasadun.com iPhone Developer Cookbook, 6.x Edition BSD License, Use at your own risk */ #include <sys/sysctl.h> NSString* getSysInfoByName(char* typeSpecifier) { size_t size; sysctlbyname(typeSpecifier, NULL, &size, NULL, 0); char *answer = malloc(size); sysctlbyname(typeSpecifier, answer, &size, NULL, 0); NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding]; free(answer); return results; } NSString* platform() { return getSysInfoByName("hw.machine"); } 

Import these functions into .pch, then you can call this one liner:

 BOOL isIphone5 = [platform() hasPrefix:@"iPhone5"]; 

It works for any device. See UIDevice-Hardware.m for a list of returned rows.

+3
source

Define the following constants in your project's .pch file

 #define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width >= 568 || [[UIScreen mainScreen] bounds].size.height >= 568)?YES:NO #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)?YES:NO #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?YES:NO #define DeviceType ((IS_IPAD) ?@ "IPAD":(IS_IPHONE5) ?@ "IPHONE 5":@"IPHONE") 

Now check your device type

 NSLog(@"%@ %@",DeviceType,[DeviceType isEqualToString:@"IPAD"] ?@ "YES":@"NO"); 

Use the following sequence to determine the type of device

 if(IS_IPAD) NSLog(@"IPAD"); else if(IS_IPHONE5) NSLog(@"IPHONE 5"); else NSLog(@"IPHONE"); 
+2
source

The best way to identify various iOS devices programmatically is through screen resolution. I did the same in my application, it works great. Please write my code.

 - (NSString *) getDeviceScreenWidth { CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGFloat width = CGRectGetWidth(screenBounds); NSNumber* number = [NSNumber numberWithFloat:width]; NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:kCFNumberFormatterDecimalStyle]; NSString* commaString = [numberFormatter stringForObjectValue:number]; NSString *screenWidth = [NSString stringWithFormat:@"%@",commaString]; NSLog(@"screen Width is: %@",screenWidth); return screenWidth; } - (NSString *) getDeviceScreenHeight { CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGFloat height = CGRectGetHeight(screenBounds); NSNumber* number = [NSNumber numberWithFloat:height]; NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:kCFNumberFormatterDecimalStyle]; NSString* commaString = [numberFormatter stringForObjectValue:number]; NSString *screenHeight = [NSString stringWithFormat:@"%@",commaString]; NSLog(@"screen height is: %@",screenHeight); return screenHeight; } 
0
source

As a sample, you can find this code in the cocos2d framework :

 -(NSInteger) runningDevice { NSInteger ret=-1; #ifdef __CC_PLATFORM_IOS if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { ret = (CC_CONTENT_SCALE_FACTOR() == 2) ? kCCDeviceiPadRetinaDisplay : kCCDeviceiPad; } else if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) { // From http://stackoverflow.com/a/12535566 BOOL isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136)); if( CC_CONTENT_SCALE_FACTOR() == 2 ) { ret = isiPhone5 ? kCCDeviceiPhone5RetinaDisplay : kCCDeviceiPhoneRetinaDisplay; } else ret = isiPhone5 ? kCCDeviceiPhone5 : kCCDeviceiPhone; } #elif defined(__CC_PLATFORM_MAC) // XXX: Add here support for Mac Retina Display ret = kCCDeviceMac; #endif // __CC_PLATFORM_MAC return ret; } 

I hope this code helps change your macro.

0
source

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


All Articles