Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Machine Type and other Hardware details through Cocoa API

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?

like image 487
coneybeare Avatar asked Nov 28 '11 16:11

coneybeare


People also ask

What language is cocoa?

Etymology 1. From Spanish cacao, from Classical Nahuatl cacahuatl. The form cocoa by confusion with coco, popularized by Samuel Johnson's A Dictionary of the English Language. Doublet of cacao.

What is iOS cocoa?

Cocoa is a set of object-oriented frameworks that provides a runtime environment for applications running in OS X and iOS. Cocoa is the preeminent application environment for OS X and the only application environment for iOS.

What is cocoa file?

Cocoa, which includes the Foundation and AppKit frameworks, is used for developing applications that run on OS X. Cocoa Touch, which includes Foundation and UIKit frameworks, is used for developing applications that run on iOS.

What is the starting point of a Cocoa application?

The first thing your interface needs is a way for the user to enter the two numbers to multiply. A basic textbox is appropriate for this purpose. Click on the "Cocoa-Text" tab of the interface elements palette. From this tab you should drag a basic text box onto the applications window.


2 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
}

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

like image 76
erkanyildiz Avatar answered Nov 09 '22 05:11

erkanyildiz


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.

like image 34
Jess the Mess Avatar answered Nov 09 '22 05:11

Jess the Mess