Retrieving Machine Type and Other Equipment Parts via Cocoa API

In iOS, there is a UIDevice class that allows you to get tons of great information about the device model, operating system, etc.

I am looking for something equivalent on a Mac. I know about a system_profilercommand line call , but even for a parameter, miniit takes a few seconds to get the information.

I'm interested in quick ways to get the type of machine (Macbook Air, Mac Mini, etc.), the OS version and other fast car parts from the Mac App. Details will be used as a footer for support letters sent from the application. Is there any equivalent UIDevice or other quick way to get some information that could help describe the user machine?

+5
source share
3 answers
#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
}

The Swift version (just swap hw.modelfor hw.machine) is here fooobar.com/questions/331873 / ...

+21
source

system_profiler SPHardwareDataType , ? . , , system_profiler -listDataTypes. , system_profiler SPSoftwareDataType.

+1

There is no built-in class of devices, as in iOS.

To learn the operating environment, you will need a Gestalt Manager .

0
source

All Articles