6

On iOS, there is a UIDevice class which lets you get tons of great info about the device model, operating system etc…

I am looking for something equivalent on the Mac. I know about calling system_profiler from the command line, but even the mini setting takes several seconds to get any info.

I am interested in fast ways to get the machine type (Macbook Air, Mac Mini, etc…), OS version, and other quick machine details from within a Mac App. The details are going o be used as a footer on support emails sent from within the app. Is there any equivalent to UIDevice, or another fast way to get some info that could help describe a user's machine?

Charles
  • 50,943
  • 13
  • 104
  • 142
coneybeare
  • 33,113
  • 21
  • 131
  • 183

4 Answers4

22
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>

+ (NSString *)machineModel
{
    size_t len = 0;
    sysctlbyname("hw.model", NULL, &len, NULL, 0);

    if (len)
    {
        char *model = malloc(len * sizeof(char));
        sysctlbyname("hw.model", model, &len, NULL, 0);
        NSString *model_ns = [NSString stringWithUTF8String:model];
        free(model);
        return model_ns;
    }

    return @"Just an Apple Computer"; //in case model name can't be read
}

Swift version (just swap hw.model for hw.machine) is here https://stackoverflow.com/a/25467259/308315

erkanyildiz
  • 13,044
  • 6
  • 50
  • 73
1

Does calling system_profiler SPHardwareDataType give you what you need? It returns very quickly and returns some basic hardware info. You can find out what other data you can request by calling system_profiler -listDataTypes. I think the other piece to your puzzle will be system_profiler SPSoftwareDataType.

Jess the Mess
  • 144
  • 1
  • 9
0

Mac has ProcessInfo class for all the Mac Machine related info

Few Use case -

  1. ProcessInfo.processInfo.operatingSystemVersion
  2. ProcessInfo.processInfo.environment (many options)

check this link https://developer.apple.com/documentation/foundation/processinfo

0

There is no built-in device class, as in iOS.

You'll need to use the Gestalt Manager to investigate the operating environment.

Macmade
  • 52,708
  • 13
  • 106
  • 123