寒山闻钟专业版 | Zero Status
iClock - Simplest Always Best | Zero Status

获取当前IOS设备的CPU型号,CPU核数,GPU,GPU核数,屏幕分辨率,屏幕尺寸,PPI等信息

原创 Zero Status 4,487 浏览 0评论

获取当前IOS设备的CPU型号,CPU核数,GPU,GPU核数,屏幕分辨率,屏幕尺寸,PPI等信息 - Blake's iOS Device Specifications Grid - 原创 | Zero Status - 1

今天和大家一起分享下如何获取当前IOS设备的CPU型号CPU核数GPUGPU核数屏幕分辨率屏幕尺寸PPI等信息。相信各位发现通过苹果官方开放的API想要获取当前设备以上的一些信息似乎做不到。如今苹果的硬件更新速度相当的快,还要在网上发现有有心人收集了所有已发布苹果设备的相关设备信息,且不定期回去更新。详情请见:Blake’s iOS Device Specifications Grid

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

获取当前IOS设备的CPU型号,CPU核数,GPU,GPU核数,屏幕分辨率,屏幕尺寸,PPI等信息 - Blake's iOS Device Specifications Grid - 原创 | Zero Status - 2

笔者比较懒,于是就借用这位有心人的设备信息来帮助我们获取iOS的以上的设备信息。下面我们就来看看代码。

首先我们为NSString添加一个Category,NSString+StringRegular。


// NSString+StringRegular.h
// HardwareInfo
//
// Created by pcjbird on 15/5/27.
// Copyright (c) 2015年 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年 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

接下来,我们来实现iOSHardwareInfoDataBase。

//
// iOSHardwareInfoDataBase.h
// HardwareInfo
//
// Created by pcjbird on 15/5/27.
// Copyright (c) 2015年 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
@property(nonatomic, readonly) BOOL isReady;
+ (iOSHardwareInfoDataBase *) sharedInstance;
-(NSDictionary *)currentDeviceInfo;
@end


//
// iOSHardwareInfoDataBase.m
// HardwareInfo
//
// Created by pcjbird on 15/5/27.
// Copyright (c) 2015年 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 @"https://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 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:IOS_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])
{
NSData *jsonData = [[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 != 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

最后我们来写下测试部分的代码。

//在程序启动时调用以下代码
[iOSHardwareInfoDataBase sharedInstance];
//在需要获取设备信息的地方调用以下代码
NSLog(@"current device info: %@", [[iOSHardwareInfoDataBase sharedInstance] currentDeviceInfo]);

希望这段分享对您有所帮助。

获取当前IOS设备的CPU型号,CPU核数,GPU,GPU核数,屏幕分辨率,屏幕尺寸,PPI等信息 - Blake's iOS Device Specifications Grid - 原创 | Zero Status - 3

获取当前IOS设备的CPU型号,CPU核数,GPU,GPU核数,屏幕分辨率,屏幕尺寸,PPI等信息 - Blake's iOS Device Specifications Grid - 原创 | Zero Status - 4

转载请注明:Zero Status » 获取当前IOS设备的CPU型号,CPU核数,GPU,GPU核数,屏幕分辨率,屏幕尺寸,PPI等信息

您必须 登录 才能发表评论!