Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions UIDevice+HardwareModel.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//
// UIDevice+HardwareModel.h
//
// Created by Heiko Dreyer on 11.05.11.
// Created by Heiko Dreyer on 05/11/11.
// Copyright 2011 boxedfolder.com. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum __UIHardwareModel
typedef NS_ENUM(NSInteger, UIHardwareModel)
{
UIHardwareModelUnknown = 0,

UIHardwareModelSimulator = 1,

UIHardwareModeliPhone1G = 2,
Expand Down Expand Up @@ -49,9 +51,12 @@ typedef enum __UIHardwareModel
UIHardwareModeliPhone5cGlobal = 29,

UIHardwareModeliPhone5s = 30,
UIHardwareModeliPhone5sGlobal = 31
UIHardwareModeliPhone5sGlobal = 31,

UIHardwareModeliPhone6 = 32,
UIHardwareModeliPhone6Plus = 33

} UIHardwareModel;
};

@interface UIDevice (HardwareModel)

Expand Down
44 changes: 29 additions & 15 deletions UIDevice+HardwareModel.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// UIDevice+HardwareModel.m
//
// Created by Heiko Dreyer on 11.05.11.
// Created by Heiko Dreyer on 05/11/11.
// Copyright 2011 boxedfolder.com. All rights reserved.
//

Expand All @@ -10,9 +10,24 @@

@implementation UIDevice (HardwareModel)

- (NSString *)rawHardwareName
{
size_t size;
char *model;

sysctlbyname("hw.machine", NULL, &size, NULL, 0);
model = malloc(size * sizeof(char));
sysctlbyname("hw.machine", model, &size, NULL, 0);

NSString *hwString = [NSString stringWithCString: model encoding: NSUTF8StringEncoding];
free(model);

return hwString;
}

- (NSString *)hardwareName
{
NSString *name = @"Unknown";
NSString *name = nil;

switch ([self hardwareModel]) {
case UIHardwareModeliPad:
Expand Down Expand Up @@ -109,28 +124,21 @@ - (NSString *)hardwareName
name = @"Simulator";
break;
default:
name = @"Unknown";
// Append the raw device name to the response to differentiate new devices
name = [NSString stringWithFormat:@"Unknown - %@", [self rawHardwareName]];
break;
}

return name;
}

-(UIHardwareModel)hardwareModel
- (UIHardwareModel)hardwareModel
{
static UIHardwareModel _hardwareModel;
static UIHardwareModel _hardwareModel = UIHardwareModelUnknown;

if(!_hardwareModel)
if (_hardwareModel == UIHardwareModelUnknown)
{
size_t size;
char *model;

sysctlbyname("hw.machine", NULL, &size, NULL, 0);
model = malloc(size);
sysctlbyname("hw.machine", model, &size, NULL, 0);

NSString *hwString = [NSString stringWithCString: model encoding: NSUTF8StringEncoding];
free(model);
NSString *hwString = [self rawHardwareName];

if([hwString isEqualToString: @"i386"] || [hwString isEqualToString:@"x86_64"])
_hardwareModel = UIHardwareModelSimulator;
Expand Down Expand Up @@ -227,6 +235,12 @@ -(UIHardwareModel)hardwareModel

if([hwString isEqualToString: @"iPad3,6"])
_hardwareModel = UIHardwareModeliPad4CDMA;

if([hwString isEqualToString: @"iPhone7,1"])
_hardwareModel = UIHardwareModeliPhone6Plus;

if([hwString isEqualToString: @"iPhone7,2"])
_hardwareModel = UIHardwareModeliPhone6;
}

return _hardwareModel;
Expand Down