下面是.m文件
//
// DeviceName.m
//
// Created by Inder Kumar Rathore on 19/01/13.
// Copyright (c) 2013 Rathore. All rights reserved.
//
#import "UIDeviceUtil.h"
#include <sys/types.h>
#include <sys/sysctl.h>
@implementation UIDeviceUtil
+ (NSString*)hardwareString {
int name[] = {CTL_HW,HW_MACHINE};
size_t size = 100;
sysctl(name, 2, NULL, &size, NULL, 0); // getting size of answer
char *hw_machine = malloc(size);
sysctl(name, 2, hw_machine, &size, NULL, 0);
NSString *hardware = [NSString stringWithUTF8String:hw_machine];
free(hw_machine);
return hardware;
}
/* This is another way of gtting the system info
* For this you have to #import <sys/utsname.h>
*/
/*
NSString* machineName
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}
*/
+ (Hardware)hardware {
NSString *hardware = [self hardwareString];
if ([hardware isEqualToString:@"iPhone1,1"]) return IPHONE_2G;
if ([hardware isEqualToString:@"iPhone1,2"]) return IPHONE_3G;
if ([hardware isEqualToString:@"iPhone2,1"]) return IPHONE_3GS;
if ([hardware isEqualToString:@"iPhone3,1"]) return IPHONE_4;
if ([hardware isEqualToString:@"iPhone3,2"]) return IPHONE_4;
if ([hardware isEqualToString:@"iPhone3,3"]) return IPHONE_4_CDMA;
if ([hardware isEqualToString:@"iPhone4,1"]) return IPHONE_4S;
if ([hardware isEqualToString:@"iPhone5,1"]) return IPHONE_5;
if ([hardware isEqualToString:@"iPhone5,2"]) return IPHONE_5_CDMA_GSM;
if ([hardware isEqualToString:@"iPhone5,3"]) return IPHONE_5C;
if ([hardware isEqualToString:@"iPhone5,4"]) return IPHONE_5C_CDMA_GSM;
if ([hardware isEqualToString:@"iPhone6,1"]) return IPHONE_5S;
if ([hardware isEqualToString:@"iPhone6,2"]) return IPHONE_5S_CDMA_GSM;
if ([hardware isEqualToString:@"iPhone7,1"]) return IPHONE_6_PLUS;
if ([hardware isEqualToString:@"iPhone7,2"]) return IPHONE_6;
if ([hardware isEqualToString:@"iPod1,1"]) return IPOD_TOUCH_1G;
if ([hardware isEqualToString:@"iPod2,1"]) return IPOD_TOUCH_2G;
if ([hardware isEqualToString:@"iPod3,1"]) return IPOD_TOUCH_3G;
if ([hardware isEqualToString:@"iPod4,1"]) return IPOD_TOUCH_4G;
if ([hardware isEqualToString:@"iPod5,1"]) return IPOD_TOUCH_5G;
if ([hardware isEqualToString:@"iPad1,1"]) return IPAD;
if ([hardware isEqualToString:@"iPad1,2"]) return IPAD_3G;
if ([hardware isEqualToString:@"iPad2,1"]) return IPAD_2_WIFI;
if ([hardware isEqualToString:@"iPad2,2"]) return IPAD_2;
if ([hardware isEqualToString:@"iPad2,3"]) return IPAD_2_CDMA;
if ([hardware isEqualToString:@"iPad2,4"]) return IPAD_2;
if ([hardware isEqualToString:@"iPad2,5"]) return IPAD_MINI_WIFI;
if ([hardware isEqualToString:@"iPad2,6"]) return IPAD_MINI;
if ([hardware isEqualToString:@"iPad2,7"]) return IPAD_MINI_WIFI_CDMA;
if ([hardware isEqualToString:@"iPad3,1"]) return IPAD_3_WIFI;
if ([hardware isEqualToString:@"iPad3,2"]) return IPAD_3_WIFI_CDMA;
if ([hardware isEqualToString:@"iPad3,3"]) return IPAD_3;
if ([hardware isEqualToString:@"iPad3,4"]) return IPAD_4_WIFI;
if ([hardware isEqualToString:@"iPad3,5"]) return IPAD_4;
if ([hardware isEqualToString:@"iPad3,6"]) return IPAD_4_GSM_CDMA;
if ([hardware isEqualToString:@"iPad4,1"]) return IPAD_AIR_WIFI;
if ([hardware isEqualToString:@"iPad4,2"]) return IPAD_AIR_WIFI_GSM;
if ([hardware isEqualToString:@"iPad4,3"]) return IPAD_AIR_WIFI_CDMA;
if ([hardware isEqualToString:@"iPad4,4"]) return IPAD_MINI_RETINA_WIFI;
if ([hardware isEqualToString:@"iPad4,5"]) return IPAD_MINI_RETINA_WIFI_CDMA;
if ([hardware isEqualToString:@"iPad4,6"]) return IPAD_MINI_RETINA_WIFI_CELLULAR_CN;
if ([hardware isEqualToString:@"iPad4,7"]) return IPAD_MINI_3_WIFI;
if ([hardware isEqualToString:@"iPad4,8"]) return IPAD_MINI_3_WIFI_CELLULAR;
if ([hardware isEqualToString:@"iPad5,3"]) return IPAD_AIR_2_WIFI;
if ([hardware isEqualToString:@"iPad5,4"]) return IPAD_AIR_2_WIFI_CELLULAR;
if ([hardware isEqualToString:@"i386"]) return SIMULATOR;
if ([hardware isEqualToString:@"x86_64"]) return SIMULATOR;
return NOT_AVAILABLE;
}
+ (NSString*)hardwareDescription {
NSString *hardware = [self hardwareString];
if ([hardware isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
if ([hardware isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([hardware isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([hardware isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (GSM)";
if ([hardware isEqualToString:@"iPhone3,2"]) return @"iPhone 4 (GSM Rev. A)";
if ([hardware isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (CDMA)";
if ([hardware isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([hardware isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)";
if ([hardware isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (Global)";
if ([hardware isEqualToString:@"iPhone5,3"]) return @"iPhone 5C (GSM)";
if ([hardware isEqualToString:@"iPhone5,4"]) return @"iPhone 5C (Global)";
if ([hardware isEqualToString:@"iPhone6,1"]) return @"iPhone 5S (GSM)";
if ([hardware isEqualToString:@"iPhone6,2"]) return @"iPhone 5S (Global)";
if ([hardware isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([hardware isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([hardware isEqualToString:@"iPod1,1"]) return @"iPod Touch (1 Gen)";
if ([hardware isEqualToString:@"iPod2,1"]) return @"iPod Touch (2 Gen)";
if ([hardware isEqualToString:@"iPod3,1"]) return @"iPod Touch (3 Gen)";
if ([hardware isEqualToString:@"iPod4,1"]) return @"iPod Touch (4 Gen)";
if ([hardware isEqualToString:@"iPod5,1"]) return @"iPod Touch (5 Gen)";
if ([hardware isEqualToString:@"iPad1,1"]) return @"iPad (WiFi)";
if ([hardware isEqualToString:@"iPad1,2"]) return @"iPad 3G";
if ([hardware isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
if ([hardware isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
if ([hardware isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
if ([hardware isEqualToString:@"iPad2,4"]) return @"iPad 2 (WiFi Rev. A)";
if ([hardware isEqualToString:@"iPad2,5"]) return @"iPad Mini (WiFi)";
if ([hardware isEqualToString:@"iPad2,6"]) return @"iPad Mini (GSM)";
if ([hardware isEqualToString:@"iPad2,7"]) return @"iPad Mini (CDMA)";
if ([hardware isEqualToString:@"iPad3,1"]) return @"iPad 3 (WiFi)";
if ([hardware isEqualToString:@"iPad3,2"]) return @"iPad 3 (CDMA)";
if ([hardware isEqualToString:@"iPad3,3"]) return @"iPad 3 (Global)";
if ([hardware isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
if ([hardware isEqualToString:@"iPad3,5"]) return @"iPad 4 (CDMA)";
if ([hardware isEqualToString:@"iPad3,6"]) return @"iPad 4 (Global)";
if ([hardware isEqualToString:@"iPad4,1"]) return @"iPad Air (WiFi)";
if ([hardware isEqualToString:@"iPad4,2"]) return @"iPad Air (WiFi+GSM)";
if ([hardware isEqualToString:@"iPad4,3"]) return @"iPad Air (WiFi+CDMA)";
if ([hardware isEqualToString:@"iPad4,4"]) return @"iPad Mini Retina (WiFi)";
if ([hardware isEqualToString:@"iPad4,5"]) return @"iPad Mini Retina (WiFi+CDMA)";
if ([hardware isEqualToString:@"iPad4,6"]) return @"iPad Mini Retina (Wi-Fi + Cellular CN)";
if ([hardware isEqualToString:@"iPad4,7"]) return @"iPad Mini 3 (Wi-Fi)";
if ([hardware isEqualToString:@"iPad4,8"]) return @"iPad Mini 3 (Wi-Fi + Cellular)";
if ([hardware isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (Wi-Fi)";
if ([hardware isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Wi-Fi + Cellular)";
if ([hardware isEqualToString:@"i386"]) return @"Simulator";
if ([hardware isEqualToString:@"x86_64"]) return @"Simulator";
NSLog(@"This is a device which is not listed in this category. Please visit https://github.com/inderkumarrathore/UIDevice-Hardware and add a comment there.");
NSLog(@"Your device hardware string is: %@", hardware);
if ([hardware hasPrefix:@"iPhone"]) return @"iPhone";
if ([hardware hasPrefix:@"iPod"]) return @"iPod";
if ([hardware hasPrefix:@"iPad"]) return @"iPad";
return nil;
}
+ (NSString*)hardwareSimpleDescription {
NSString *hardware = [self hardwareString];
if ([hardware isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
if ([hardware isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([hardware isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([hardware isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([hardware isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
if ([hardware isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
if ([hardware isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([hardware isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
if ([hardware isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
if ([hardware isEqualToString:@"iPhone5,3"]) return @"iPhone 5C";
if ([hardware isEqualToString:@"iPhone5,4"]) return @"iPhone 5C";
if ([hardware isEqualToString:@"iPhone6,1"]) return @"iPhone 5S";
if ([hardware isEqualToString:@"iPhone6,2"]) return @"iPhone 5S";
if ([hardware isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([hardware isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([hardware isEqualToString:@"iPod1,1"]) return @"iPod Touch (1 Gen)";
if ([hardware isEqualToString:@"iPod2,1"]) return @"iPod Touch (2 Gen)";
if ([hardware isEqualToString:@"iPod3,1"]) return @"iPod Touch (3 Gen)";
if ([hardware isEqualToString:@"iPod4,1"]) return @"iPod Touch (4 Gen)";
if ([hardware isEqualToString:@"iPod5,1"]) return @"iPod Touch (5 Gen)";
if ([hardware isEqualToString:@"iPad1,1"]) return @"iPad";
if ([hardware isEqualToString:@"iPad1,2"]) return @"iPad";
if ([hardware isEqualToString:@"iPad2,1"]) return @"iPad 2";
if ([hardware isEqualToString:@"iPad2,2"]) return @"iPad 2";
if ([hardware isEqualToString:@"iPad2,3"]) return @"iPad 2";
if ([hardware isEqualToString:@"iPad2,4"]) return @"iPad 2";
if ([hardware isEqualToString:@"iPad2,5"]) return @"iPad Mini";
if ([hardware isEqualToString:@"iPad2,6"]) return @"iPad Mini";
if ([hardware isEqualToString:@"iPad2,7"]) return @"iPad Mini";
if ([hardware isEqualToString:@"iPad3,1"]) return @"iPad 3";
if ([hardware isEqualToString:@"iPad3,2"]) return @"iPad 3";
if ([hardware isEqualToString:@"iPad3,3"]) return @"iPad 3";
if ([hardware isEqualToString:@"iPad3,4"]) return @"iPad 4";
if ([hardware isEqualToString:@"iPad3,5"]) return @"iPad 4";
if ([hardware isEqualToString:@"iPad3,6"]) return @"iPad 4";
if ([hardware isEqualToString:@"iPad4,1"]) return @"iPad Air";
if ([hardware isEqualToString:@"iPad4,2"]) return @"iPad Air";
if ([hardware isEqualToString:@"iPad4,3"]) return @"iPad Air";
if ([hardware isEqualToString:@"iPad4,4"]) return @"iPad Mini Retina";
if ([hardware isEqualToString:@"iPad4,5"]) return @"iPad Mini Retina";
if ([hardware isEqualToString:@"iPad4,6"]) return @"iPad Mini Retina CN";
if ([hardware isEqualToString:@"iPad4,7"]) return @"iPad Mini 3";
if ([hardware isEqualToString:@"iPad4,8"]) return @"iPad Mini 3";
if ([hardware isEqualToString:@"iPad5,3"]) return @"iPad Air 2";
if ([hardware isEqualToString:@"iPad5,4"]) return @"iPad Air 2";
if ([hardware isEqualToString:@"i386"]) return @"Simulator";
if ([hardware isEqualToString:@"x86_64"]) return @"Simulator";
NSLog(@"This is a device which is not listed in this category. Please visit https://github.com/inderkumarrathore/UIDevice-Hardware and add a comment there.");
NSLog(@"Your device hardware string is: %@", hardware);
if ([hardware hasPrefix:@"iPhone"]) return @"iPhone";
if ([hardware hasPrefix:@"iPod"]) return @"iPod";
if ([hardware hasPrefix:@"iPad"]) return @"iPad";
return nil;
}
+ (float)hardwareNumber:(Hardware)hardware {
switch (hardware) {
case IPHONE_2G: return 1.1f;
case IPHONE_3G: return 1.2f;
case IPHONE_3GS: return 2.1f;
case IPHONE_4: return 3.1f;
case IPHONE_4_CDMA: return 3.3f;
case IPHONE_4S: return 4.1f;
case IPHONE_5: return 5.1f;
case IPHONE_5_CDMA_GSM: return 5.2f;
case IPHONE_5C: return 5.3f;
case IPHONE_5C_CDMA_GSM: return 5.4f;
case IPHONE_5S: return 6.1f;
case IPHONE_5S_CDMA_GSM: return 6.2f;
case IPHONE_6_PLUS: return 7.1f;
case IPHONE_6: return 7.2f;
case IPOD_TOUCH_1G: return 1.1f;
case IPOD_TOUCH_2G: return 2.1f;
case IPOD_TOUCH_3G: return 3.1f;
case IPOD_TOUCH_4G: return 4.1f;
case IPOD_TOUCH_5G: return 5.1f;
case IPAD: return 1.1f;
case IPAD_3G: return 1.2f;
case IPAD_2_WIFI: return 2.1f;
case IPAD_2: return 2.2f;
case IPAD_2_CDMA: return 2.3f;
case IPAD_MINI_WIFI: return 2.5f;
case IPAD_MINI: return 2.6f;
case IPAD_MINI_WIFI_CDMA: return 2.7f;
case IPAD_3_WIFI: return 3.1f;
case IPAD_3_WIFI_CDMA: return 3.2f;
case IPAD_3: return 3.3f;
case IPAD_4_WIFI: return 3.4f;
case IPAD_4: return 3.5f;
case IPAD_4_GSM_CDMA: return 3.6f;
case IPAD_AIR_WIFI: return 4.1f;
case IPAD_AIR_WIFI_GSM: return 4.2f;
case IPAD_AIR_WIFI_CDMA: return 4.3f;
case IPAD_AIR_2_WIFI: return 5.3f;
case IPAD_AIR_2_WIFI_CELLULAR: return 5.4f;
case IPAD_MINI_RETINA_WIFI: return 4.4f;
case IPAD_MINI_RETINA_WIFI_CDMA: return 4.5f;
case IPAD_MINI_3_WIFI: return 4.6f;
case IPAD_MINI_3_WIFI_CELLULAR: return 4.7f;
case IPAD_MINI_RETINA_WIFI_CELLULAR_CN: return 4.8f;
case SIMULATOR: return 100.0f;
case NOT_AVAILABLE: return 200.0f;
}
return 200.0f; //Device is not available
}
+ (BOOL)isCurrentDeviceHardwareBetterThan:(Hardware)hardware {
float otherHardware = [self hardwareNumber:hardware];
float currentHardware = [self hardwareNumber:[self hardware]];
return currentHardware >= otherHardware;
}
+ (CGSize)backCameraStillImageResolutionInPixels
{
switch ([self hardware]) {
case IPHONE_2G:
case IPHONE_3G:
return CGSizeMake(1600, 1200);
break;
case IPHONE_3GS:
return CGSizeMake(2048, 1536);
break;
case IPHONE_4:
case IPHONE_4_CDMA:
case IPAD_3_WIFI:
case IPAD_3_WIFI_CDMA:
case IPAD_3:
case IPAD_4_WIFI:
case IPAD_4:
case IPAD_4_GSM_CDMA:
return CGSizeMake(2592, 1936);
break;
case IPHONE_4S:
case IPHONE_5:
case IPHONE_5_CDMA_GSM:
case IPHONE_5C:
case IPHONE_5C_CDMA_GSM:
return CGSizeMake(3264, 2448);
break;
case IPOD_TOUCH_4G:
return CGSizeMake(960, 720);
break;
case IPOD_TOUCH_5G:
return CGSizeMake(2440, 1605);
break;
case IPAD_2_WIFI:
case IPAD_2:
case IPAD_2_CDMA:
return CGSizeMake(872, 720);
break;
case IPAD_MINI_WIFI:
case IPAD_MINI:
case IPAD_MINI_WIFI_CDMA:
return CGSizeMake(1820, 1304);
break;
case IPAD_AIR_2_WIFI:
case IPAD_AIR_2_WIFI_CELLULAR:
return CGSizeMake (1536, 2048);
break;
default:
NSLog(@"We have no resolution for your device's camera listed in this category. Please, make photo with back camera of your device, get its resolution in pixels (via Preview Cmd+I for example) and add a comment to this repository on GitHub.com in format Device = Hpx x Wpx.");
NSLog(@"Your device is: %@", [self hardwareDescription]);
break;
}
return CGSizeZero;
}
@end
下面是.h文件
//
// DeviceName.h
//
// Created by Inder Kumar Rathore on 19/01/13.
// Copyright (c) 2013 Rathore. All rights reserved.
//
#import <UIKit/UIKit.h>
#define IS_IPAD (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
#define DEVICE_HARDWARE_BETTER_THAN(i) [UIDeviceUtil isCurrentDeviceHardwareBetterThan:i]
#define DEVICE_HAS_RETINA_DISPLAY (fabs([UIScreen mainScreen].scale - 2.0) <= fabs([UIScreen mainScreen].scale - 2.0)*DBL_EPSILON)
#define IS_SCREEN_HEIGHT_EQUAL(SCREEN_HEIGHT_VALUE) (MAX([UIScreen mainScreen].bounds.size.height,[UIScreen mainScreen].bounds.size.width)==SCREEN_HEIGHT_VALUE)
#define IS_IPHONE_5 ( IS_IPHONE && IS_SCREEN_HEIGHT_EQUAL(568.0) )
#define IS_SIMULATOR ( [UIDeviceUtil hardware]==SIMULATOR )
#define IS_IPHONE_6 ( IS_IPHONE && ([UIDeviceUtil hardware]==IPHONE_6 || IS_SCREEN_HEIGHT_EQUAL(667.0)) )
#define IS_IPHONE_6_PLUS ( IS_IPHONE && ([UIDeviceUtil hardware]==IPHONE_6_PLUS || IS_SCREEN_HEIGHT_EQUAL(736.0)) )
typedef NS_ENUM(int32_t, Hardware)
{
NOT_AVAILABLE,
IPHONE_2G,
IPHONE_3G,
IPHONE_3GS,
IPHONE_4,
IPHONE_4_CDMA,
IPHONE_4S,
IPHONE_5,
IPHONE_5_CDMA_GSM,
IPHONE_5C,
IPHONE_5C_CDMA_GSM,
IPHONE_5S,
IPHONE_5S_CDMA_GSM,
IPHONE_6_PLUS,
IPHONE_6,
IPOD_TOUCH_1G,
IPOD_TOUCH_2G,
IPOD_TOUCH_3G,
IPOD_TOUCH_4G,
IPOD_TOUCH_5G,
IPAD,
IPAD_2,
IPAD_2_WIFI,
IPAD_2_CDMA,
IPAD_3,
IPAD_3G,
IPAD_3_WIFI,
IPAD_3_WIFI_CDMA,
IPAD_4,
IPAD_4_WIFI,
IPAD_4_GSM_CDMA,
IPAD_MINI,
IPAD_MINI_WIFI,
IPAD_MINI_WIFI_CDMA,
IPAD_MINI_RETINA_WIFI,
IPAD_MINI_RETINA_WIFI_CDMA,
IPAD_MINI_3_WIFI,
IPAD_MINI_3_WIFI_CELLULAR,
IPAD_MINI_RETINA_WIFI_CELLULAR_CN,
IPAD_AIR_WIFI,
IPAD_AIR_WIFI_GSM,
IPAD_AIR_WIFI_CDMA,
IPAD_AIR_2_WIFI,
IPAD_AIR_2_WIFI_CELLULAR,
SIMULATOR
};
@interface UIDeviceUtil : NSObject
/** This method retruns the hardware type */
+ (NSString*)hardwareString;
/** This method returns the Hardware enum depending upon harware string */
+ (Hardware)hardware;
/** This method returns the readable description of hardware string */
+ (NSString*)hardwareDescription;
/** This method returs the readble description without identifier (GSM, CDMA, GLOBAL) */
+ (NSString *)hardwareSimpleDescription;
/** This method returns YES if the current device is better than the hardware passed */
+ (BOOL)isCurrentDeviceHardwareBetterThan:(Hardware)hardware;
/** This method returns the resolution for still image that can be received
from back camera of the current device. Resolution returned for image oriented landscape right. **/
+ (CGSize)backCameraStillImageResolutionInPixels;
@end
网友评论