Get the current iOS device CPU model, CPU cores, GPU,GPU, screen resolution, screen size, PPI and more

Source: Internet
Author: User

Share with you today how to get the current iOS device CPU model ,CPU cores ,GPU,GPU cores , screen resolution , screen size , PPI and other information. I'm sure you'll find that the API, which is officially open through Apple, wants to get some information above the current device. Now Apple's hardware update speed is quite fast, but also on the internet to find a conscientious collection of all the published Apple devices related equipment information , and do not regularly go back to update. For details, see: Blake's IOS Device Specifications Grid

Http://www.blakespot.com/ios_device_specifications_grid.html

The author is lazy, so I borrowed this conscientious device information to help us get more IOS device information . Let's take a look at the code below.

First we add a category,nsstring+stringregular for nsstring.

////  nsstring+stringregular.h//  hardwareinfo////  created by  pcjbird on 15/5/27.//  copyright  (c)  2015 year  Zero Status. All  rights reserved.//#import  <Foundation/Foundation.h> @interface  NSString  ( Stringregular)-(nsmutablearray *) Substringbyregular: (nsstring *) regular; @end 
  nsstring+stringregular.m//  hardwareinfo////  created by pcjbird  on 15/5/27.//  Copyright  (c)  2015 year  zero status. all rights  reserved.//#import   "Nsstring+stringregular.h" @implementation  NSString  (stringregular)-( nsmutablearray *) Substringbyregular: (nsstring *) regular{         nsstring * reg=regular;        nsrange r= [ self rangeofstring:reg options:nsregularexpressionsearch];         nsmutablearray *arr=[nsmutablearray array];        if   (r.length != nsnotfound &&r.length != 0)  {                 while  (r.length !=  nsnotfound &&r.length != 0)  {             nsstring* substr = [self substringwithrange:r];             [arr addObject:substr];             nsrange startr=nsmakerange (r.location+r.length, [self length]- R.location-r.length);            r=[self  rangeofstring:reg options:nsregularexpressionsearch range:startr];         }    }    return arr;} @end

Next, let's implement ioshardwareinfodatabase.

////  ioshardwareinfodatabase.h//  hardwareinfo////  created by  pcjbird on 15/5/27.//  copyright  (c)  2015 year  Zero Status. All  rights reserved.//#import  <Foundation/Foundation.h>/*********************************  device, model id, year, soc, ram  (MB), mem speed  (MHz),  Mem  Type, CPU, CPU Arch, Data Width, CPU Cores, CPU Clock  (MHz), geekbench score, gpu, gpu cores, gpu clock  (MHz),  Screen  Res, PPI, Screen Size  (inches)  *********************************/@interface  iOSHardwareInfoDataBase : NSObject+  (ioshardwareinfodatabase *)  sharedInstance; @property (nonatomic, readonly)  bool isready;-(nsdictionary *) currentdeviceinfo; @end 
 ioshardwareinfodatabase.m// hardwareinfo//// created by pcjbird on 15/ 5/27.// copyright  (c)  2015 year  zero status. all rights reserved.//#import   "IOSHardwareInfoDataBase.h" #import   "Nsstring+stringregular.h" #include  <sys/socket.h># include <sys/sysctl.h> #include  <net/if.h> #include  <net/if_dl.h> #define   ios_device_specifications_grid_url @ "http://www.blakespot.com/ios_device_specifications_grid.html" @ Interface ioshardwareinfodatabase () {    bool _isready;} @property (Nonatomic, strong)  NSMutableDictionary* db; @end @implementation  ioshardwareinfodatabase@synthesize isready = _isready;+  (iOSHardwareInfoDataBase *)  sharedinstance{    static ioshardwareinfodatabase* _sharedinstance =  nil;    static dispatch_once_t&nbsP;oncetoken;    dispatch_once (&onceToken, ^{         if  (!_sharedinstance)  {             if  (!_sharedinstance)  _sharedInstance=[[iOSHardwareInfoDataBase alloc] init];         }    });     return  _sharedinstance;} -(ID) init{    if  (self =[super init])  {         _isready = false;        self.db =  [nsmutabledictionary dictionary];        dispatch_async ( Dispatch_get_global_queue (0, 0),  ^ (void) {             [self tryparse];        });     }     return self;} -(nsstring *) getmetadata{    nsurl *url = [nsurl urlwithstring:i os_device_specifications_grid_url];    nsdata *data = [nsdata  Datawithcontentsofurl:url];    return [[nsstring alloc] initwithdata:data  encoding:nsutf8stringencoding];} -(void)  TryParse{    NSFileManager*fileManager =[NSFileManager  Defaultmanager];    nsarray*paths =nssearchpathfordirectoriesindomains ( Nsdocumentdirectory,nsuserdomainmask, yes);     nsstring*documentsdirectory =[ paths objectatindex:0];    nsstring*path =[documentsdirectory  stringbyappendingpathcomponent:@ "Hardware"];    if ([Filemanager fileexistsatpath:path ]) &NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;NSDATA&NBSP;*JSONDATA&Nbsp;= [[nsfilemanager defaultmanager] contentsatpath:path];         NSDictionary * db= [jsonData toJSONObject];         [self.db addEntriesFromDictionary:db];    }     nsstring *metadata = [self getmetadata];    if  (!metaData)      {        _isReady = TRUE;         return;    }    NSRange  rangetable= [metadata rangeofstring:@ "<table border=0 cellpadding=0 cellspacing=2 >[\\s\\S]*</table>  options:NSRegularExpressionSearch];    if  ( rangetable.length <=0)     {        _ Isready = true;        return;    }    [self.db  removeallobjects];    nsstring* tablestr = [[metadata  substringwithrange:rangetable] stringbyreplacingoccurrencesofstring:@ " "  withstring:@ ""];     tablestr = [tablestr stringbyreplacingoccurrencesofstring:@ "<br>"   withstring:@ " "];    tablestr = [tablestr  stringbyreplacingoccurrencesofstring:@ "# "  withstring:@ ""];         nsmutablearray *infoarray=[tablestr substringbyregular:@ "<tr[\\s\\S]*?>[\\s\\S]*? </tr> "];    nsstring *titlestr = [infoarray firstobject];     nsmutablearray *titlearr = [titlestr substringbyregular:@ "<td[\\s\\S] *?>[\\s\\s]*?</td> "];        for  (nsuinteger i = 0; i < [titlearr count]; i++)     {        nsstring *title = [ titlearr objectatindex:i];        nsrange rangetd =  [title rangeofstring:@ "<td[\\s\\S]*?>"  options:NSRegularExpressionSearch];         title = [title stringbyreplacingcharactersinrange:rangetd  withstring:@ ""];        title = [title  stringbyreplacingoccurrencesofstring:@ "</td>"  withstring:@ " "];         [titleArr replaceObjectAtIndex:i withObject:title];    }         [infoarray removeobjectatindex:0];    [ infoarray removelastobject];        for  (Nsstring *deviceinfo in infoarray)      {        nsmutablearray *devices = [ deviceinfo substringbyregular:@ "<td[\\s\\S]*?>[\\s\\S]*?</td>"];                 for  (nsuinteger i =  0; i < [devices count]; i++)                   {              NSString *val = [devices objectAtIndex:i];              nsrange rangetd = [val rangeofstring : @ "<td[\\s\\S]*?>"  options:NSRegularExpressionSearch];              val = [val stringbyreplacingcharactersinrange:rangetd withstring:@ ""];              val = [val  stringbyreplacingoccurrencesofstring:@ "</td>"  withstring:@ " "];              [devices replaceObjectAtIndex:i withObject:val];          }          nsstring *devicekey = [devices objectatindex:1];         nsarray* keyarr = [[devicekey stringbyreplacingoccurrencesofstring:@ " "   withstring:@ ""] componentsseparatedbystring:@ "?"];          NSRange chRange = [[keyArr  firstobject] rangeofstring:@ "[\\d]*"  options:NSRegularExpressionSearch];         NSString* chs = [[keyArr firstObject]  substringwithrange:chrange];         for  (NSString* _ Key in keyarr)          {              if  (_key && [_key length]  > 0)             {                 nsmutabledictionary *dict =  [NSMutableDictionary dictionary];                 for  (nsuinteger j = 0; j < [titlearr  count]; j++)                  {                    if  (J &NBSP;!=&NBSP;1)                      {                         nsstring *titlekey = [[ titlearr objectatindex:j] stringbytrimmingcharactersinset:[nscharacterset  whitespacecharacterset]];                         nsstring *deviceval = [[devices  objectAtIndex:j]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];;                          [dict setobject:deviceval forkey:titlekey];                     }                 }                 NSString* dKey = _key;                 if  ([dkey rangeofstring:chs].length == 0)  dkey  = [chs stringByAppendingString:dKey];                 [self.db setObject:dict forKey:dKey];             }        }     }    _isready = true;    nsdata * Newjsondata = [dataparseutil tojsondata:self.db];    [newjsondata writetofile:path  Atomically:yes];} -  (nsstring *)  platform{    size_t size;     Sysctlbyname ("Hw.machine",  null, &size, null, 0);     char * Machine = malloc (size);     sysctlbyname ("Hw.machine", machine, & size, null, 0);    nsstring *platform = [nsstring  Stringwithcstring:machine encoding:nsutf8stringencoding];    free (Machine);     return platform;} -(nsdictionary *) currentdeviceinfo{    if  (_isready)     {         return [self.db objectForKey:[self platform]];     }    return nil;} @end

Finally, let's write down the code for the test section.

Call the following code when the program starts [Ioshardwareinfodatabase sharedinstance]; Call the following code where you need to obtain the device information NSLog (@ "Current device info:%@", [[Ioshardwareinfodatabase sharedinstance] currentdeviceinfo]);

I hope this share is helpful to you.


Get the current iOS device CPU model, CPU cores, GPU,GPU, screen resolution, screen size, PPI and more

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.