博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS中判断设备型号的宏定义
阅读量:5154 次
发布时间:2019-06-13

本文共 4108 字,大约阅读时间需要 13 分钟。

一、做成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”);
}

 

 

 

 
 
 

转载于:https://www.cnblogs.com/fcihpy/p/4648113.html

你可能感兴趣的文章
BZOJ2243 洛谷2486 [SDOI2011]染色 树链剖分
查看>>
centos 引导盘
查看>>
JS绘制曲线图
查看>>
Notes of Daily Scrum Meeting(12.8)
查看>>
在Eclipse中查看JDK类库的源代码
查看>>
linux每日命令(32):gzip命令
查看>>
第三次作业
查看>>
Apriori算法
查看>>
onlevelwasloaded的调用时机
查看>>
求出斐波那契数组
查看>>
Vue.js 基础学习之组件通信
查看>>
Java程序员的成长之路
查看>>
lr_start_transaction/lr_end_transaction事物组合
查看>>
那些React-Native踩过的的坑
查看>>
jcomboBox显示长项目的内容
查看>>
qml----Model/View入门(三)ListView分组显示
查看>>
DXP Altium Ddesigner的各种栅格(grid)意义及设置 分类: ...
查看>>
Atitit。Cas机制 软件开发 编程语言 无锁机制 java c# php
查看>>
posix信号量(sem_t)
查看>>
12.1日常
查看>>