版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.07.15 |
前言
在app中,我们经常有需求就是获取设备信息,这一篇就说一下方法。感兴趣的可以看看我写的其他小技巧。
1. 实用小技巧(一):UIScrollView中上下左右滚动方向的判断
2. 实用小技巧(二):屏幕横竖屏的判断和相关逻辑
3.实用小技巧(三):点击手势屏蔽子视图的响应
4.实用小技巧(四):动态的增删标签视图
5.实用小技巧(五):通过相册或者相机更改图标
6.实用小技巧(六):打印ios里所有字体
7. 实用小技巧(七):UITableViewCell自适应行高的计算
8. 实用小技巧(八):数字余额显示的分隔
9.实用小技巧(九):类头条模糊背景的实现
10.实用小技巧(十):晃动手机换后台服务器网络
11.实用小技巧(十一):scrollView及其子类显示的一些异常处理
12.实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现
13.实用小技巧(十三):一种类酷我音乐盒动画实现
14.实用小技巧(十四):生成跳往applestore指定app的方法
15.实用小技巧(十五):左侧向右滑动返回上一级控制器
功能需求
很多时候我们需要进行获取设备信息,比如对不同的设备进行适配,又比如对于不同的设备获取视频的分辨率进行差别化设置。
功能实现
这里我写的是UIDevce
的分类,使用的时候直接调用对象方法就可以使用。我们先看代码。
UIDevice+ZBCustomUIDeviceCategory.h
#import <UIKit/UIKit.h>
extern NSString *const ZebraUIDevice_unrecognized;
extern NSString *const ZebraUIDevice_simulator;
extern NSString *const ZebraUIDevice_ipod1;
extern NSString *const ZebraUIDevice_ipod2;
extern NSString *const ZebraUIDevice_ipod3;
extern NSString *const ZebraUIDevice_ipod4;
extern NSString *const ZebraUIDevice_ipod5;
extern NSString *const ZebraUIDevice_ipad2;
extern NSString *const ZebraUIDevice_ipad3;
extern NSString *const ZebraUIDevice_ipad4;
extern NSString *const ZebraUIDevice_ipadMini1;
extern NSString *const ZebraUIDevice_ipadMini2;
extern NSString *const ZebraUIDevice_ipadMini3;
extern NSString *const ZebraUIDevice_ipadAir1;
extern NSString *const ZebraUIDevice_ipadAir2;
extern NSString *const ZebraUIDevice_iphone4;
extern NSString *const ZebraUIDevice_iphone4S;
extern NSString *const ZebraUIDevice_iphone5;
extern NSString *const ZebraUIDevice_iphone5C;
extern NSString *const ZebraUIDevice_iphone5S;
extern NSString *const ZebraUIDevice_iphone6;
extern NSString *const ZebraUIDevice_iphone6PLUS;
extern NSString *const ZebraUIDevice_iphone6S;
extern NSString *const ZebraUIDevice_iphone6SPLUS;
extern NSString *const ZebraUIDevice_iphone7;
extern NSString *const ZebraUIDevice_iphone7PLUS;
@interface UIDevice (ZBCustomUIDeviceCategory)
- (NSString *)zbDeviceModel;
@end
UIDevice+ZBCustomUIDeviceCategory.m
#import "UIDevice+ZBCustomUIDeviceCategory.h"
#import <sys/utsname.h>
NSString *const ZebraUIDevice_unrecognized = @"unrecognized";
NSString *const ZebraUIDevice_simulator = @"simulator";
NSString *const ZebraUIDevice_ipod1 = @"ipod1";
NSString *const ZebraUIDevice_ipod2 = @"ipod2";
NSString *const ZebraUIDevice_ipod3 = @"ipod3";
NSString *const ZebraUIDevice_ipod4 = @"ipod4";
NSString *const ZebraUIDevice_ipod5 = @"ipod5";
NSString *const ZebraUIDevice_ipad2 = @"ipad2";
NSString *const ZebraUIDevice_ipad3 = @"ipad3";
NSString *const ZebraUIDevice_ipad4 = @"ipad4";
NSString *const ZebraUIDevice_ipadMini1 = @"ipadMini1";
NSString *const ZebraUIDevice_ipadMini2 = @"ipadMini2";
NSString *const ZebraUIDevice_ipadMini3 = @"ipadMini3";
NSString *const ZebraUIDevice_ipadAir1 = @"ipadAir1";
NSString *const ZebraUIDevice_ipadAir2 = @"ipadAir2";
NSString *const ZebraUIDevice_iphone4 = @"iphone4";
NSString *const ZebraUIDevice_iphone4S = @"iphone4S";
NSString *const ZebraUIDevice_iphone5 = @"iphone5";
NSString *const ZebraUIDevice_iphone5C = @"iphone5C";
NSString *const ZebraUIDevice_iphone5S = @"iphone5S";
NSString *const ZebraUIDevice_iphone6 = @"iphone6";
NSString *const ZebraUIDevice_iphone6PLUS = @"iphone6Plus";
NSString *const ZebraUIDevice_iphone6S = @"iphone6S";
NSString *const ZebraUIDevice_iphone6SPLUS = @"iphone6sPlus";
NSString *const ZebraUIDevice_iphone7 = @"iphone7";
NSString *const ZebraUIDevice_iphone7PLUS = @"iphone7Plus";
@implementation UIDevice (ZBCustomUIDeviceCategory)
#pragma mark - 获得设备名称
- (NSString *)zbDeviceModel
{
struct utsname systemInfo;
uname(&systemInfo);
NSString *codeStr = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
static NSDictionary *deviceNameByCode = nil;
if (!deviceNameByCode) {
deviceNameByCode = @{
@"i386" : ZebraUIDevice_simulator,
@"x86_64" : ZebraUIDevice_simulator,
@"iPod1,1" : ZebraUIDevice_ipod1,
@"iPod2,1" : ZebraUIDevice_ipod2,
@"iPod3,1" : ZebraUIDevice_ipod3,
@"iPod4,1" : ZebraUIDevice_ipod4,
@"iPod5,1" : ZebraUIDevice_ipod5,
@"iPad2,1" : ZebraUIDevice_ipad2,
@"iPad2,2" : ZebraUIDevice_ipad2,
@"iPad2,3" : ZebraUIDevice_ipad2,
@"iPad2,4" : ZebraUIDevice_ipad2,
@"iPad2,5" : ZebraUIDevice_ipad2,
@"iPad2,6" : ZebraUIDevice_ipad2,
@"iPad2,7" : ZebraUIDevice_ipad2,
@"iPad3,1" : ZebraUIDevice_ipad3,
@"iPad3,2" : ZebraUIDevice_ipad3,
@"iPad3,3" : ZebraUIDevice_ipad3,
@"iPad3,4" : ZebraUIDevice_ipad4,
@"iPad3,5" : ZebraUIDevice_ipad4,
@"iPad3,6" : ZebraUIDevice_ipad4,
@"iPad4,1" : ZebraUIDevice_ipadAir1,
@"iPad4,2" : ZebraUIDevice_ipadAir2,
@"iPad4,4" : ZebraUIDevice_ipadMini2,
@"iPad4,5" : ZebraUIDevice_ipadMini2,
@"iPad4,6" : ZebraUIDevice_ipadMini2,
@"iPad4,7" : ZebraUIDevice_ipadMini3,
@"iPad4,8" : ZebraUIDevice_ipadMini3,
@"iPad4,9" : ZebraUIDevice_ipadMini3,
@"iPhone3,1" : ZebraUIDevice_iphone4,
@"iPhone3,2" : ZebraUIDevice_iphone4,
@"iPhone3,3" : ZebraUIDevice_iphone4,
@"iPhone4,1" : ZebraUIDevice_iphone4S,
@"iPhone5,1" : ZebraUIDevice_iphone5,
@"iPhone5,2" : ZebraUIDevice_iphone5,
@"iPhone5,3" : ZebraUIDevice_iphone5C,
@"iPhone5,4" : ZebraUIDevice_iphone5C,
@"iPhone6,1" : ZebraUIDevice_iphone5S,
@"iPhone6,2" : ZebraUIDevice_iphone5S,
@"iPhone7,1" : ZebraUIDevice_iphone6PLUS,
@"iPhone7,2" : ZebraUIDevice_iphone6,
@"iPhone8,1" : ZebraUIDevice_iphone6S,
@"iPhone8,2" : ZebraUIDevice_iphone6SPLUS,
@"iPhone9,1" : ZebraUIDevice_iphone7,
@"iPhone9,2" : ZebraUIDevice_iphone7PLUS,
};
}
NSString *deviceNameStr = [deviceNameByCode objectForKey:codeStr];
if (deviceNameStr) {
return deviceNameStr;
}
return ZebraUIDevice_unrecognized;
}
@end
下面我们就看一下使用方法。
下面就是我适配的只使用的获取设备型号,差异化适配。
[self.videoDescriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.videoView).offset(kZBShortVideoTeamMainZoneCellVideoDescriptionLeftMargin);
make.bottom.equalTo(self.contentView).offset(-15);
if ([[[UIDevice currentDevice] zbDeviceModel] isEqualToString:ZebraUIDevice_iphone6] ||
[[[UIDevice currentDevice] zbDeviceModel] isEqualToString:ZebraUIDevice_iphone6S] ||
[[[UIDevice currentDevice] zbDeviceModel] isEqualToString:ZebraUIDevice_iphone7]) {
make.width.equalTo(@(320 * 0.5));
}
else if ([[[UIDevice currentDevice] zbDeviceModel] isEqualToString:ZebraUIDevice_iphone6PLUS] ||
[[[UIDevice currentDevice] zbDeviceModel] isEqualToString:ZebraUIDevice_iphone6SPLUS] ||
[[[UIDevice currentDevice] zbDeviceModel] isEqualToString:ZebraUIDevice_iphone7PLUS]){
make.width.equalTo(@(460 * 0.5));
}
else {
make.width.equalTo(@(155.0));
}
}];
后记
这个工具还算好用,大家可能还有别的方法,我自己封装的就这个方法了,希望对大家有所帮助。