Determine the iOS device model

Source: Internet
Author: User

The judging code is as follows:

-(NSString *) getmachine
{
size_t size;

Implicitly declared function Sysctlbyname is not valid in C99
int NR;
= Sysctlbyname ("Hw.machine", NULL, &size, NULL,
0);
Char *machine = (char *) malloc (size);

NR = Sysctlbyname ("Hw.machine", Machine, &size, NULL, 0);

NSString *platform = [NSString stringwithcstring:machine encoding:nsasciistringencoding];

Free (machine);

if ([Platform isequaltostring:@ "iphone1,1"]) return @ "IPhone 2G (A1203)";
if ([Platform isequaltostring:@ "iphone1,2"]) return @ "IPhone 3G (a1241/a1324)";
if ([Platform isequaltostring:@ "iphone2,1"]) return @ "IPhone 3GS (a1303/a1325)";
if ([Platform isequaltostring:@ "iphone3,1"]) return @ "IPhone 4 (A1332)";
if ([Platform isequaltostring:@ "iphone3,2"]) return @ "IPhone 4 (A1332)";
if ([Platform isequaltostring:@ "iphone3,3"]) return @ "IPhone 4 (A1349)";
if ([Platform isequaltostring:@ "iphone4,1"]) return @ "IPhone 4S (a1387/a1431)";
if ([Platform isequaltostring:@ "iphone5,1"]) return @ "IPhone 5 (A1428)";
if ([Platform isequaltostring:@ "iphone5,2"]) return @ "IPhone 5 (a1429/a1442)";
if ([Platform isequaltostring:@ "iphone5,3"]) return @ "IPhone 5c (a1456/a1532)";
if ([Platform isequaltostring:@ "iphone5,4"]) return @ "IPhone 5c (a1507/a1516/a1526/a1529)";
if ([Platform isequaltostring:@ "iphone6,1"]) return @ "IPhone 5s (a1453/a1533)";
if ([Platform isequaltostring:@ "iphone6,2"]) return @ "IPhone 5s (a1457/a1518/a1528/a1530)";
if ([Platform isequaltostring:@ "iphone7,1"]) return @ "IPhone 6 Plus (a1522/a1524)";
if ([Platform isequaltostring:@ "iphone7,2"]) return @ "IPhone 6 (a1549/a1586)";

if ([Platform isequaltostring:@ "ipod1,1"]) return @ "IPod Touch 1G (A1213)";
if ([Platform isequaltostring:@ "ipod2,1"]) return @ "IPod Touch 2G (A1288)";
if ([Platform isequaltostring:@ "ipod3,1"]) return @ "IPod Touch 3G (A1318)";
if ([Platform isequaltostring:@ "ipod4,1"]) return @ "IPod Touch 4G (A1367)";
if ([Platform isequaltostring:@ "ipod5,1"]) return @ "IPod Touch 5G (a1421/a1509)";

if ([Platform isequaltostring:@ "ipad1,1"]) return @ "IPad 1G (a1219/a1337)";

if ([Platform isequaltostring:@ "ipad2,1"]) return @ "IPad 2 (A1395)";
if ([Platform isequaltostring:@ "ipad2,2"]) return @ "IPad 2 (A1396)";
if ([Platform isequaltostring:@ "ipad2,3"]) return @ "IPad 2 (A1397)";
if ([Platform isequaltostring:@ "ipad2,4"]) return @ "IPad 2 (a1395+new Chip)";
if ([Platform isequaltostring:@ "ipad2,5"]) return @ "IPad Mini 1G (A1432)";
if ([Platform isequaltostring:@ "ipad2,6"]) return @ "IPad Mini 1G (A1454)";
if ([Platform isequaltostring:@ "ipad2,7"]) return @ "IPad Mini 1G (A1455)";

if ([Platform isequaltostring:@ "ipad3,1"]) return @ "IPad 3 (A1416)";
if ([Platform isequaltostring:@ "ipad3,2"]) return @ "IPad 3 (A1403)";
if ([Platform isequaltostring:@ "ipad3,3"]) return @ "IPad 3 (A1430)";
if ([Platform isequaltostring:@ "ipad3,4"]) return @ "IPad 4 (A1458)";
if ([Platform isequaltostring:@ "ipad3,5"]) return @ "IPad 4 (A1459)";
if ([Platform isequaltostring:@ "ipad3,6"]) return @ "IPad 4 (A1460)";

if ([Platform isequaltostring:@ "ipad4,1"]) return @ "IPad Air (A1474)";
if ([Platform isequaltostring:@ "ipad4,2"]) return @ "IPad Air (A1475)";
if ([Platform isequaltostring:@ "ipad4,3"]) return @ "IPad Air (A1476)";
if ([Platform isequaltostring:@ "ipad4,4"]) return @ "IPad Mini 2G (A1489)";
if ([Platform isequaltostring:@ "ipad4,5"]) return @ "IPad Mini 2G (A1490)";
if ([Platform isequaltostring:@ "ipad4,6"]) return @ "IPad Mini 2G (A1491)";

if ([Platform isequaltostring:@ "i386"]) return @ "IPhone Simulator";
if ([Platform isequaltostring:@ "x86_64"]) return @ "IPhone Simulator";
return platform;
}

where NR = Sysctlbyname ("Hw.machine", Machine, &size, NULL, 0); This function is not valid in C99, judging whether the current compiler is C99 standard, do the following:

 #include <stdio.h>  
int Main ()
#if (!defined (__ stdc__))
printf ("non-standard C! ");
#elif defined (__stdc_version__)
printf ("Standard C version:%ld. ", __stdc_version__);
#else
printf (" old standard C. ");
#endif
GetChar ();
return 0;

predefined macro __stdc__ represents standard C. __STDC_VERSION__ represents the standard C version. The C99 corresponds to 199901L.

struct Utsname systemInfo;
Uname (&systeminfo);
NSString *devicestring = [NSString stringWithCString:systemInfo.machine encoding:nsutf8stringencoding];
NSLog (@ "%@", devicestring);

This function can also achieve the effect,




Determine the iOS device model

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.