2019独角兽企业重金招聘Python工程师标准>>>
今天和大家一起分享下如何获取当前IOS设备的CPU型号,CPU核数,GPU,GPU核数,屏幕分辨率,屏幕尺寸,PPI等信息。相信各位发现通过苹果官方开放的API想要获取当前设备以上的一些信息似乎做不到。如今苹果的硬件更新速度相当的快,还要在网上发现有有心人收集了所有已发布苹果设备的相关设备信息,且不定期回去更新。详情请见:Blake’s iOS Device Specifications Grid
http://www.blakespot.com/ios_device_specifications_grid.html
笔者比较懒,于是就借用这位有心人的设备信息来帮助我们获取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
@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 /*********************************
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年 Zero Status. All rights reserved.
//#import "iOSHardwareInfoDataBase.h"
#import "NSString+StringRegular.h"
#include
#include
#include
#include
#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 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:@"[\\s\\S]*
" options:NSRegularExpressionSearch];
if (rangeTable.length <=0)
{
_isReady = TRUE;
return;
}
[self.db removeAllObjects];
NSString* tableStr = [[metaData substringWithRange:rangeTable] stringByReplacingOccurrencesOfString:@" " withString:@""];
tableStr = [tableStr stringByReplacingOccurrencesOfString:@"
" withString:@" "];
tableStr = [tableStr stringByReplacingOccurrencesOfString:@"# " withString:@""];
NSMutableArray *infoArray=[tableStr substringByRegular:@"[\\s\\S]*? "];
NSString *titleStr = [infoArray firstObject];
NSMutableArray *titleArr = [titleStr substringByRegular:@"[\\s\\S]*? "];
for (NSUInteger i = 0; i < [titleArr count]; i++)
{
NSString *title = [titleArr objectAtIndex:i];
NSRange rangetd = [title rangeOfString:@"" options:NSRegularExpressionSearch];
title = [title stringByReplacingCharactersInRange:rangetd withString:@""];
title = [title stringByReplacingOccurrencesOfString:@" " withString:@" "];
[titleArr replaceObjectAtIndex:i withObject:title];
}
[infoArray removeObjectAtIndex:0];
[infoArray removeLastObject];
for (NSString *deviceInfo in infoArray)
{
NSMutableArray *devices = [deviceInfo substringByRegular:@"[\\s\\S]*? "];
for (NSUInteger i = 0; i < [devices count]; i++)
{
NSString *val = [devices objectAtIndex:i];
NSRange rangetd = [val rangeOfString:@"" options:NSRegularExpressionSearch];
val = [val stringByReplacingCharactersInRange:rangetd withString:@""];
val = [val stringByReplacingOccurrencesOfString:@" " 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]);
希望这段分享对您有所帮助。