Skip to content

Commit 269a2ef

Browse files
committedNov 12, 2017
• Added feature to get Mac hardware model string.
1 parent 5cbe6f4 commit 269a2ef

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
 

‎SSYSystemDescriber.h

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
+ (SSYVersionTriplet*)softwareVersionTriplet ;
1111
+ (NSString*)softwareVersionString ;
1212

13+
/*!
14+
@result Returns, for example, "MacBookAir6,2"
15+
*/
16+
+ (NSString*)hardwareModel;
17+
1318
/*!
1419
@brief Returns system speed relative to a 2009 Mac Mini with
1520
Core 2 Duo processor.

‎SSYSystemDescriber.m

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "SSYSystemDescriber.h"
22
#import "SSYVersionTriplet.h"
3+
#import <sys/sysctl.h>
34

45
@implementation SSYSystemDescriber
56

@@ -39,6 +40,24 @@ + (SSYVersionTriplet*)softwareVersionTriplet {
3940
bugFix:bugFix] ;
4041
}
4142

43+
/* Thanks to erkanyildiz. See https://stackoverflow.com/questions/8299087/getting-the-machine-type-and-other-hardware-details-through-cocoa-api */
44+
+ (NSString *) hardwareModel {
45+
size_t len = 0;
46+
sysctlbyname("hw.model", NULL, &len, NULL, 0);
47+
48+
NSString* answer;
49+
if (len > 0) {
50+
char* model = malloc(len*sizeof(char));
51+
sysctlbyname("hw.model", model, &len, NULL, 0);
52+
answer = [NSString stringWithUTF8String:model];
53+
free(model);
54+
} else {
55+
answer = @"Could not get Mac model";
56+
}
57+
58+
return answer;
59+
}
60+
4261
+ (NSString*)softwareVersionString {
4362
return [NSString stringWithFormat:@"macOS version = %@",
4463
[[self softwareVersionTriplet] string]] ;

0 commit comments

Comments
 (0)
Please sign in to comment.