1、前言node
今天作一個功能,須要動態的得到啓動頁,而後根據不一樣設備去使用不用的啓動頁圖片。ios
2、正文 測試
常規來講,咱們直接判斷是何種設備,而後經過name去得到圖片選擇性加載便可。可是實際上遇到的兩個問題。this
第一:如何判斷各類設備。google
第二:怎麼去拿到啓動頁圖片。spa
先看第一個問題,怎麼去判斷各類設備。原來習慣性的是直接判斷設備高度。閒着沒事就經過code
[UIScreen mainScreen].bounds.size.height
這個方法去得到各類設備的高度,以下:orm
//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==480) { // return nil; return @"iPhone4/iPhone4s"; }else if (screenHeight==568) { return @"iPhone5/iPhone5s/iPhone5c"; }else if (screenHeight==667) { return @"iPhone6/iPhone6s"; }else if (screenHeight==736) { return @"iPhone6 plus/iPhone6s plus"; }else { return @"another"; } }
其實這是一個很low的方法,只是根據高度去判斷,不過這樣也能夠去判斷。get
下面來看看方法二:
首先,咱們須要在使用的類裏面導入:
#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,2
當時比較疑惑,這是什麼狀況呢?而後就google了一下。原來這個是設備的類型。並且不一樣的設備類型也不同。而後就在這裏發現了下面的內容:(其實這個結果就是設備類型)
//模擬器 @"i386" on 32-bit Simulator @"x86_64" on 64-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 4 (GSM) @"iPhone3,2" on iPhone 4 @"iPhone3,3" on iPhone 4 (CDMA/Verizon/Sprint) @"iPhone4,1" on iPhone 4S @"iPhone5,1" on iPhone 5 (model A1428, AT&T/Canada) @"iPhone5,2" on iPhone 5 (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 6 Plus @"iPhone7,2" on iPhone 6 @"iPhone8,1" on iPhone 6S @"iPhone8,2" on iPhone 6S Plus //iPad --不全面 @"iPad1,1" on iPad @"iPad2,1" on iPad 2 @"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"];
來拿到啓動頁面。其餘的同樣。
這樣就拿到了啓動頁面。
綜合起來解決前言問題的方法就是:
//方法一<br>- (UIImage *)getLaunchImage { CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; if (screenHeight==480) { // return nil; return [UIImage imageNamed: @"LaunchImage-700"]; }else if (screenHeight==568) { return [UIImage imageNamed: @"LaunchImage-700-568h"]; }else if (screenHeight==667) { return [UIImage imageNamed: @"LaunchImage-800-667h"]; }else if (screenHeight==736) { 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"]; //這裏我用了一張默認圖片 } }