一、前言
今天做一个功能,需要动态的获得启动页,然后根据不同设备去使用不用的启动页图片。
二、正文
常规来说,我们直接判断是何种设备,然后通过name去获得图片选择性加载即可。但是实际上遇到的两个问题。
第一:如何判断各种设备。
第二:怎么去拿到启动页图片。
先看第一个问题,怎么去判断各种设备。原来习惯性的是直接判断设备高度。闲着没事就通过
[UIScreen mainScreen].bounds.size.height
这个方法去获得各种设备的高度,如下:
//iPhone4s 480
//iPhone 5 568
//iPhone 5s 568
//iPhone 6 667
//iPhone 6p 736
//iPhone 6s 667
//iPhone 6sp 736
因此做了一个判断方法一:
- (NSString *)getCurrentDeviceVersion {
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if (screenHeight==) {
// return nil;
return @"iPhone4/iPhone4s";
}else if (screenHeight==) {
return @"iPhone5/iPhone5s/iPhone5c";
}else if (screenHeight==) {
return @"iPhone6/iPhone6s";
}else if (screenHeight==) {
return @"iPhone6 plus/iPhone6s plus";
}else {
return @"another";
} }
其实这是一个很low的方法,只是根据高度去判断,不过这样也可以去判断。
下面来看看方法二:
首先,我们需要在使用的类里面导入:
#import <sys/utsname.h>
- (NSString *)getDeviceVersion {
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
if ([deviceName isEqualToString:@"iPhone3,1"] || [deviceName isEqualToString:@"iPhone3,3"]|| [deviceName isEqualToString:@"iPhone4,1"]) { //判断是否是iPhone4,iPhone4s
return @"iPhone4/iPhone4s";
}else if ([deviceName isEqualToString:@"iPhone5,1"]|| [deviceName isEqualToString:@"iPhone5,2"]||[deviceName isEqualToString:@"iPhone5,3"] || [deviceName isEqualToString:@"iPhone5,4"] || [deviceName isEqualToString:@"iPhone6,1"] || [deviceName isEqualToString:@"iPhone6,2"]) {
return @"iPhone5/iPhone5s/iPhone5c";
}else if ([deviceName isEqualToString:@"iPhone7,1"] || [deviceName isEqualToString:@"iPhone8,2"]) {
return @"iPhone6 plus/iPhone6s plus";
}else if ([deviceName isEqualToString:@"iPhone7,2"] || [deviceName isEqualToString:@"iPhone8,1"]) {
return @"iPhone6/iPhone6s";
}else { //真机上边不会走到
return @"another";
}
}
方法二主要用到了一个类utsname.h。打开之后,我们可以发现他就是一个结构体:
struct utsname {
char sysname[_SYS_NAMELEN]; /* [XSI] Name of OS */
char nodename[_SYS_NAMELEN]; /* [XSI] Name of this network node */
char release[_SYS_NAMELEN]; /* [XSI] Release level */
char version[_SYS_NAMELEN]; /* [XSI] Version level */
char machine[_SYS_NAMELEN]; /* [XSI] Hardware type */
};
先来看看怎么去取到
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
当输出deviceName的时候,你就会发现不同的设备将会输出不同的内容,但是当我们使用模拟器的时候,会输出:
x86_64
其实这个是在64-bit Simulator下输出的。如果我们使用的是32-bit Simulator,那么输出的结果就是
i386
然后我使用iPhone5s进行测试,输出结果是:
iPhone6,
当时比较疑惑,这是什么情况呢?然后就google了一下。原来这个是设备的类型。而且不同的设备类型也不一样。然后就在这里发现了下面的内容:(其实这个结果就是设备类型)
//模拟器 @"i386" on -bit Simulator
@"x86_64" on -bit Simulator //iPod--可能不全面 @"iPod1,1" on iPod Touch
@"iPod2,1" on iPod Touch Second Generation
@"iPod3,1" on iPod Touch Third Generation
@"iPod4,1" on iPod Touch Fourth Generation
@"iPod5,1" on iPod Touch 5th Generation
@"iPod7,1" on iPod Touch 6th Generation //iPhone --全面了 @"iPhone1,1" on iPhone
@"iPhone1,2" on iPhone 3G
@"iPhone2,1" on iPhone 3GS
@"iPhone3,1" on iPhone (GSM)
@"iPhone3,2" on iPhone
@"iPhone3,3" on iPhone (CDMA/Verizon/Sprint)
@"iPhone4,1" on iPhone 4S
@"iPhone5,1" on iPhone (model A1428, AT&T/Canada)
@"iPhone5,2" on iPhone (model A1429, everything else)
@"iPhone5,3" on iPhone 5c (model A1456, A1532 | GSM)
@"iPhone5,4" on iPhone 5c (model A1507, A1516, A1526 (China), A1529 | Global)
@"iPhone6,1" on iPhone 5s (model A1433, A1533 | GSM)
@"iPhone6,2" on iPhone 5s (model A1457, A1518, A1528 (China), A1530 | Global)
@"iPhone7,1" on iPhone Plus
@"iPhone7,2" on iPhone
@"iPhone8,1" on iPhone 6S
@"iPhone8,2" on iPhone 6S Plus //iPad --不全面 @"iPad1,1" on iPad
@"iPad2,1" on iPad
@"iPad3,1" on 3rd Generation iPad
@"iPad3,4" on 4th Generation iPad
@"iPad2,5" on iPad Mini
@"iPad4,1" on 5th Generation iPad (iPad Air) - Wifi
@"iPad4,2" on 5th Generation iPad (iPad Air) - Cellular
@"iPad4,4" on 2nd Generation iPad Mini - Wifi
@"iPad4,5" on 2nd Generation iPad Mini - Cellular
@"iPad4,7" on 3rd Generation iPad Mini - Wifi (model A1599)
这里几乎包括了所有的iOS移动设备。
然后就根据这个去判断了一下
- (NSString *)getDeviceModel {
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
if ([deviceName isEqualToString:@"iPhone3,1"] || [deviceName isEqualToString:@"iPhone3,3"]|| [deviceName isEqualToString:@"iPhone4,1"]) { //判断是否是iPhone4,iPhone4s
return @"iPhone4/iPhone4s";
}else if ([deviceName isEqualToString:@"iPhone5,1"]|| [deviceName isEqualToString:@"iPhone5,2"]||[deviceName isEqualToString:@"iPhone5,3"] || [deviceName isEqualToString:@"iPhone5,4"] || [deviceName isEqualToString:@"iPhone6,1"] || [deviceName isEqualToString:@"iPhone6,2"]) {
return @"iPhone5/iPhone5s/iPhone5c";
}else if ([deviceName isEqualToString:@"iPhone7,1"] || [deviceName isEqualToString:@"iPhone8,2"]) {
return @"iPhone6 plus/iPhone6s plus";
}else if ([deviceName isEqualToString:@"iPhone7,2"] || [deviceName isEqualToString:@"iPhone8,1"]) {
return @"iPhone6/iPhone6s";
}else { //真机上边不会走到
return @"another";
}
}
这样就得到了不同版本的设备。有兴趣的话还可以点这里(可能需要越狱)
这样,第一个问题就结局了。第二个问题是如何获得当前project的启动页面。(不要妄想去通过名字拿,试了,拿不到。)
后来google了一下:得到如下
//iPhone4/4s:[UIImage imageNamed: @"LaunchImage-700"];
//
//iPhone5/5s/5c: [UIImage imageNamed: @"LaunchImage-700-568h"];
//
//iPhone6: [UIImage imageNamed: @"LaunchImage-800-667h"];
//
//iPhone6 plus 竖屏:[UIImage imageNamed: @"LaunchImage-800-Portrait-736h"];
//
//iPhone6 plus 横屏:[UIImage imageNamed: @"LaunchImage-800-Portrait-736h"];
意思就是当你是iPhone4/4s的时候,就通过
[UIImage imageNamed: @"LaunchImage-700"];
来拿到启动页面。其他的一样。
这样就拿到了启动页面。
综合起来解决前言问题的方法就是:
//方法一
- (UIImage *)getLaunchImage {
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if (screenHeight==) {
// return nil;
return [UIImage imageNamed: @"LaunchImage-700"];
}else if (screenHeight==) {
return [UIImage imageNamed: @"LaunchImage-700-568h"];
}else if (screenHeight==) {
return [UIImage imageNamed: @"LaunchImage-800-667h"];
}else if (screenHeight==) {
return [UIImage imageNamed: @"LaunchImage-800-Portrait-736h"];
}else {
return [UIImage imageNamed:@"placeH"];
}
} /**
* 通过系统信息得到版本
*方法二
* @return 指定的LaunchImage
*/
- (UIImage *)getLaunchImageByiPhoneType {
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
if ([deviceName isEqualToString:@"iPhone3,1"] || [deviceName isEqualToString:@"3,2"] ||[deviceName isEqualToString:@"iPhone3,3"]|| [deviceName isEqualToString:@"iPhone4,1"]) { //判断是否是iPhone4,iPhone4s
return [UIImage imageNamed:@"LaunchImage-700"];
}else if ([deviceName isEqualToString:@"iPhone5,1"]|| [deviceName isEqualToString:@"iPhone5,2"]||[deviceName isEqualToString:@"iPhone5,3"] || [deviceName isEqualToString:@"iPhone5,4"] || [deviceName isEqualToString:@"iPhone6,1"] || [deviceName isEqualToString:@"iPhone6,2"]) {
return [UIImage imageNamed: @"LaunchImage-700-568h"];
}else if ([deviceName isEqualToString:@"iPhone7,1"] || [deviceName isEqualToString:@"iPhone8,2"]) {
return [UIImage imageNamed: @"LaunchImage-800-Portrait-736h"];
}else if ([deviceName isEqualToString:@"iPhone7,2"] || [deviceName isEqualToString:@"iPhone8,1"]) {
return [UIImage imageNamed: @"LaunchImage-800-667h"];
}else { //真机上边不会走到
return [UIImage imageNamed:@"placeH"]; //这里我用了一张默认图片
}
}
三、结语
如果大家有什么疑问或者我说的不对的地方尽管提出来。