How IOS gets screen width, device model, system version information _ios

Source: Internet
Author: User

Introduced
While I was learning about Android development, I think the device fit is a headache, there are too many Android devices, then I envy the iOS developers do not have to worry about adaptation, and when I began to learn the development of iOS, iOS screen also began to be a variety of things ... So I have to do the adaptation, sad ...
I've studied it before, and here's my method. This article describes three commonly used device information acquisition methods:

    • Gets the width and height of the screen. Used to calculate the distance relative to the screen when setting the position of the control
    • Gets the model number of the device. 5s and 6+ screen size is very far, the corresponding control position, size needs to make adjustments, otherwise it will appear in the 6+ is very empty or on the 5s display incomplete problem.
    • Gets the system version. Different system versions have different characteristics, for example, with a chestnut, there is no live Photo in the version below iOS 9, and a chestnut, the system version of iOS 7 more often needs to adjust the border

The next details of the acquisition method, in fact, are relatively simple.

1, get the width of the screen height

The width of the screen is an often needed message, especially if you write the UI in code. For example, when you write a Uilabel, set its frame, you want it to be centered, and you want to set the width to 200, how do you set its x value? That is (the width of the screen-200)/2, so you can guarantee that it will always be centered on whatever device. The way to get the screen wide and high is as follows:

Device width 
 [uiscreen mainscreen].bounds.size.width 
 
//equipment height 
[UIScreen mainscreen].bounds.size.height 

In general we set it as a macro in a PCH file so that it can be invoked everywhere, without having to use a string of code each time:

Width and height of the device 
#define ScreenWidth    [uiscreen mainscreen].bounds.size.width 
#define ScreenHeight   [ UIScreen Mainscreen].bounds.size.height 

This allows you to use the macro screenwidth and screenheight directly where you need it.

2, get the model of the equipment

There are several ways to get a device model, where I use a more stupid method to get the resolution of the device to judge the model of the device. Let's look at the following table first:

Attention to the device resolution of the column, we can see several screens of the device resolution is different, so we can use this as a basis for judging the device model, I have judged some of the most common models, the same use of macros:

To determine the device based on the screen resolution, return Yes, not return no 
#define ISIPHONE5OR5SOR5C ([UIScreen instancesrespondtoselector: @selector ( CurrentMode)]? Cgsizeequaltosize (Cgsizemake (640, 1136), [[UIScreen Mainscreen] currentmode].size): NO) 
#define ISIPHONE6OR6S ([ UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (750, 1334), [[UIScreen Mainscreen] currentmode].size): NO 
#define Isiphone6plusor6splus ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (1242, 2208), [[UIScreen Mainscreen] currentmode].size): NO) 

So I just need to determine which of these values is yes, and I can tell which is the current model, such as:

if (isiphone5or5sor5c) { 
  NSLog (@ "This is iPhone5 or 5s or 5c"); 
} else if (isiphone6or6s) { 
  NSLog (@ "This is IPhone6 or 6s" ); 
} else if (isiphone6plusor6splus) { 
  NSLog (@ "This is Iphone6plus or 6splus"); 
} 

That's fine.
In fact, the corresponding ipad, itouch, etc. can also be so judged, as long as the corresponding resolution to find a good judge.

3. Get System version

Getting the system version also applies to macros for easy global invocation:

The system version of the device 
#define SYSTEMVERSION ([[Uidevice Currentdevice] systemversion]) 

So you can get the version number, you can print it out:
NSLog (@ "Current operating system is: ios%@", systemversion);

When used, it can be converted to the number of float type to judge, such as:

if ([Systemversion Floatvalue] >= 7.0) 
{ 
  ... 
} 

The common method of acquiring equipment information is here, I hope to help everyone ~
This is my example project at GitHub. Address: Https://github.com/Cloudox/GetDeviceInfoDemo, Welcome to star and fork~

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.