一、做成device的分类
h文件
#import <UIKit/UIKit.h>
typedef NS_ENUM (NSUInteger, FCDeviceType) {
DeviceUnknown = 0, DeviceIsSimulator = 1, DeviceIsiPhone = 2, DeviceIsiPhone3G = 3, DeviceIsiPhone3GS = 4, DeviceIsiPhone4 = 5, DeviceIsiPhone4S = 6, DeviceIsiPhone5 = 7, DeviceIsiPhone5C = 8, DeviceIsiPhone5S = 9, DeviceIsiPhone6 = 10, DeviceIsiPhone6Plus = 11, DeviceIsiPodTouch = 12, DeviceIsiPodTouch2G = 13, DeviceIsiPodTouch3G = 14, DeviceIsiPodTouch4G = 15, DeviceIsiPad = 16, DeviceIsiPad2 = 17, DeviceIsiPad3G = 18, DeviceIsiPad4G = 19, DeviceIsiPad5G_Air = 20, DeviceIsiPadMini = 21, DeviceIsiPadMini2G = 22};@interface UIDevice (FC)
/** * 判断设备型号 * * @return 返回设备型号的枚举类型 */
- (FCDeviceType)deviceType;
m文件
#pragma mark 从系统底层获取硬件平台类型字符串 - (NSString*)platformString{ size_t size; sysctlbyname("hw.machine",NULL, &size, NULL, 0); char *machine = (char*)malloc(size); sysctlbyname("hw.machine", machine, &size,NULL, 0); NSString *platform = [NSStringstringWithCString:machineencoding:NSUTF8StringEncoding]; NSString *PhoneDeviceStr = [NSStringstring]; PhoneDeviceStr = [UIDevicecurrentDevice].model; free(machine); return platform;
}
#pragma mark 获取设备型号的枚举类型 - (FCDeviceType)deviceType{ NSString *platform = [self platformString]; FCDeviceType deviceType; //iPhone if ([platform isEqualToString:@"iPhone1,2"]) return DeviceIsiPad3G; if ([platform isEqualToString:@"iPhone2,1"]) return DeviceIsiPhone3GS; if ([platform isEqualToString:@"iPhone3,1"]) return DeviceIsiPhone4; if ([platform isEqualToString:@"iPhone3,2"]) return DeviceIsiPhone4; if ([platform isEqualToString:@"iPhone3,3"]) return DeviceIsiPhone4; if ([platform isEqualToString:@"iPhone4,1"]) return DeviceIsiPhone4S; if ([platform isEqualToString:@"iPhone5,1"]) return DeviceIsiPhone5; if ([platform isEqualToString:@"iPhone5,2"]) return DeviceIsiPhone5; if ([platform isEqualToString:@"iPhone5,3"]) return DeviceIsiPhone5C; if ([platform isEqualToString:@"iPhone5,4"]) return DeviceIsiPhone5C; if ([platform isEqualToString:@"iPhone6,1"]) return DeviceIsiPhone5S; if ([platform isEqualToString:@"iPhone6,2"]) return DeviceIsiPhone5S; if ([platform isEqualToString:@"iPhone7,1"]) return DeviceIsiPhone6; if ([platform isEqualToString:@"iPhone7,2"]) return DeviceIsiPhone6Plus; //iPod Touch if ([platform isEqualToString:@"iPod1,1"]) return DeviceIsiPodTouch; if ([platform isEqualToString:@"iPod2,1"]) return DeviceIsiPodTouch2G; if ([platform isEqualToString:@"iPod3,1"]) return DeviceIsiPodTouch3G; if ([platform isEqualToString:@"iPod4,1"]) return DeviceIsiPodTouch4G;
//iPad if ([platform isEqualToString:@"iPad1,1"]) return DeviceIsiPad; if ([platform isEqualToString:@"iPad2,1"]) return DeviceIsiPad2; if ([platform isEqualToString:@"iPad2,2"]) return DeviceIsiPad2; if ([platform isEqualToString:@"iPad2,3"]) return DeviceIsiPad2; if ([platform isEqualToString:@"iPad2,4"]) return DeviceIsiPad2; if ([platform isEqualToString:@"iPad2,5"]) return DeviceIsiPadMini; if ([platform isEqualToString:@"iPad2,6"]) return DeviceIsiPadMini; if ([platform isEqualToString:@"iPad2,7"]) return DeviceIsiPadMini; if ([platform isEqualToString:@"iPad3,1"]) return DeviceIsiPad3G; if ([platform isEqualToString:@"iPad3,2"]) return DeviceIsiPad3G; if ([platform isEqualToString:@"iPad3,3"]) return DeviceIsiPad3G; if ([platform isEqualToString:@"iPad3,4"]) return DeviceIsiPad4G; if ([platform isEqualToString:@"iPad3,5"]) return DeviceIsiPad4G; if ([platform isEqualToString:@"iPad3,6"]) return DeviceIsiPad4G;
if ([platform isEqualToString:@"i386"] || [platformisEqualToString:@"x86_64"]) return DeviceIsSimulator; return deviceType;
}
二、宏定义
// 1. 判断设备型号 #define kIphone4 [UIScreen mainScreen].bounds.size.height ==480#define kIphone4S [[UIDevice currentDevice] deviceType] ==6#define kIphone5 [UIScreen mainScreen].bounds.size.height ==568#define kIphone5s [[UIDevice currentDevice] deviceType] ==9#define kIphone6 [[UIDevice currentDevice] deviceType] ==11
#define kIphone6Plus [[UIDevice currentDevice] deviceType] == 10
三、使用方式
在用到地方输入判断设备型号的宏,如判断iPhone6,
if( kIphone6){
NSLog(@“this is iphone6”);
}