Get the iPhone device + how to get the boot page image,
I. Preface
To create a function today, You need to dynamically obtain the boot page and then use the Unused boot page Image Based on different devices.
Ii. Text
In general, we can directly determine the device and then obtain the image for selective loading through the name. But there are actually two problems.
First, how to determine various devices.
2. How to get the boot page image.
First, let's look at the first question. How can we determine various devices. It is customary to directly determine the device height. Pass when you are idle
[UIScreen mainScreen].bounds.size.height
This method gets the height of various devices as follows:
//iPhone4s 480//iPhone 5 568//iPhone 5s 568//iPhone 6 667//iPhone 6p 736//iPhone 6s 667//iPhone 6sp 736
Therefore, we made a judgment method 1:
- (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"; }}
In fact, this is a very low method, but it can be determined based on the height.
Let's take a look at Method 2:
First, we need to import in the class used:
# Import <sys/utsname. h>-(NSString *) getDeviceVersion {struct utsname systemInfo; uname (& systemInfo); NSString * deviceName = [NSString stringWithCString: systemInfo. machine encoding: NSUTF8StringEncoding]; if ([deviceName is%tostring: @ "iPhone3, 1"] | [deviceName is%tostring: @ "iPhone3, 3"] | [deviceName is%tostring: @ "iPhone4, 1"]) {// determines if it is iPhone4, iPhone4s return @ "iPhone4/iPhone4s";} else if ([deviceName isw.tostring: @ "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 {// The real machine does not go to return @ "another ";}}
Method 2 mainly uses a class utsname. h. After opening it, we can find that it is a struct:
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 */};
First, let's see how to get it.
struct utsname systemInfo; uname(&systemInfo); NSString *deviceName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
When the deviceName is output, you will find that different devices will output different contents, but when we use the simulator, the output will be:
x86_64
In fact, this is output under 64-bit Simulator. If we use 32-bit Simulator, the output result is
i386
Then I use the iPhone 5S for testing. The output result is:
iPhone6,2
At that time, I was confused. What is the situation? Then google. It turns out to be the device type. Different device types are also different. Then we found the following content: (in fact, the result is the device type)
// Simulator @ "i386" on 32-bit Simulator @ "x86_64" on 64-bit Simulator // iPod -- may not be comprehensive @ "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-All @" 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 6 s @ "iPhone8, 2 "on iPhone 6 S Plus // iPad -- Incomplete @" 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 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)
Almost all iOS Mobile devices are included here.
Then I decided based on this.
-(NSString *) getDeviceModel {struct utsname systemInfo; uname (& systemInfo); NSString * deviceName = [NSString stringWithCString: systemInfo. machine encoding: NSUTF8StringEncoding]; if ([deviceName is%tostring: @ "iPhone3, 1"] | [deviceName is%tostring: @ "iPhone3, 3"] | [deviceName is%tostring: @ "iPhone4, 1"]) {// determines if it is iPhone4, iPhone4s return @ "iPhone4/iPhone4s";} else if ([deviceName isw.tostring: @ "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 {// The real machine does not go to return @ "another ";}}
In this way, devices of different versions are obtained. If you are interested, click here (you may need to jailbreak)
In this way, the first problem will end. The second question is how to obtain the startup page of the current project. (Don't try to get it by name. If you try it, you won't get it .)
Then google:
// IPhone4/4 s: [UIImage imageNamed: @ "LaunchImage-700"]; // iPhone5/5S/5c: [UIImage imageNamed: @ "LaunchImage-700-568h"]; /// iPhone6: [UIImage imageNamed: @ "LaunchImage-800-667h"]; // iPhone6 plus portrait screen: [UIImage imageNamed: @ "LaunchImage-800-Portrait-736h"]; /// iPhone6 plus landscape screen: [UIImage imageNamed: @ "LaunchImage-800-Portrait-736h"];
This means that when you are using iPhone 4/4s
[UIImage imageNamed: @"LaunchImage-700"];
To get the startup page. Others are the same.
In this way, the startup page is obtained.
The methods to solve the preface problem are as follows:
// Method 1-(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"];} /*** obtain the version through system information * method 2 * @ return specified LaunchImage */-(UIImage *) getLaunchImageByiPhoneType {struct utsname systemInfo; uname (& systemInfo ); NSString * deviceName = [NSString stringWithCString: systemInfo. machine encoding: NSUTF8StringEncoding]; if ([deviceName is%tostring: @ "iPhone3, 1"] | [deviceName is%tostring: @ ""] | [deviceName is%tostring: @ "iPhone3, 3 "] | [deviceName isw.tostring: @" iPhone4, 1 "]) {// determine if it is 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 is%tostring: @" iPhone7, 1 "] | [deviceName is%tostring: @ "iPhone8, 2"]) {return [UIImage imageNamed: @ "LaunchImage-800-Portrait-736h"];} else if ([deviceName isw.tostring: @ "iPhone7, 2 "] | [deviceName isw.tostring: @" iPhone8, 1 "]) {return [UIImage imageNamed: @" LaunchImage-800-667h "];} else {// The real machine will not go to return [UIImage imageNamed: @ "placeH"]; // here I use a default image }}
Iii. Conclusion
If you have any questions or are not correct, let's raise them.