Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've been using the next line in my constants to differentiate between devices and get back the number of the device. What's the appropriate way to identify iPhone 5 and still keep it in a one 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 keep it in a one line format for all devices.

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

  1. A single-line macro that returns either @"3", @"4", or @"5" depending on the iOS device.
  2. The 4" devices (currently iPhone 5 and 5th gen iPod touch) should return @"5".
  3. All iPads and all remaining retina iPhones and iPod touches should return @"4".
  4. All remaining non-retina iPhones and iPod touches should return @"3".
like image 662
Segev Avatar asked May 06 '13 06:05

Segev


2 Answers

According to your question I'm assuming you want to identify the hardware device, not the iOS version.

/*
Erica Sadun, http://ericasadun.com
iPhone Developer's 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 those functions in the .pch, then you are free to call this one liner:

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

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

like image 65
Jano Avatar answered Sep 19 '22 01:09

Jano


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" screened iPhones and iPod touches. This will return @"4" for all iPads and retina iPhones and iPod touches. And it will return @"3" for non-retina iPhones and iPod touches.

like image 39
rmaddy Avatar answered Sep 22 '22 01:09

rmaddy