usuario tienes tiene tengo saber recuperar puedo puede pro porque permiso operaciĆ³n necesario macbook mac disco desbloquear contraseƱa completar como candado administrador macos cocoa

macos - tienes - Obtener el nombre de la computadora de mi Mac



recuperar usuario administrador mac (6)

¿Cómo puedo obtener el nombre de la computadora en una Mac? Estoy hablando del mismo nombre que el que puede encontrar en System Profiler en "Software".


Aquí hay uno que no bloquea:

NSString* name = [(NSString*)CSCopyMachineName() autorelease];


Usando SystemConfiguration.framework , que debe agregar a su proyecto:

#include <SystemConfiguration/SystemConfiguration.h> ... // Returns NULL/nil if no computer name set, or error occurred. OSX 10.1+ NSString *computerName = [(NSString *)SCDynamicStoreCopyComputerName(NULL, NULL) autorelease]; // Returns NULL/nil if no local hostname set, or error occurred. OSX 10.2+ NSString *localHostname = [(NSString *)SCDynamicStoreCopyLocalHostName(NULL) autorelease];


Yo uso sysctlbyname ("kern.hostname"), que no bloquea. Tenga en cuenta que mi método de ayuda solo debe usarse para recuperar atributos de cadena, no enteros.

#include <sys/sysctl.h> - (NSString*) systemInfoString:(const char*)attributeName { size_t size; sysctlbyname(attributeName, NULL, &size, NULL, 0); // Get the size of the data. char* attributeValue = malloc(size); int err = sysctlbyname(attributeName, attributeValue, &size, NULL, 0); if (err != 0) { NSLog(@"sysctlbyname(%s) failed: %s", attributeName, strerror(errno)); free(attributeValue); return nil; } NSString* vs = [NSString stringWithUTF8String:attributeValue]; free(attributeValue); return vs; } - (NSString*) hostName { NSArray* components = [[self systemInfoString:"kern.hostname"] componentsSeparatedByString:@"."]; return [components][0]; }


en la terminal lo tienes con:

system_profiler SPSoftwareDataType | grep "Computer Name" | cut -d: -f2 | tr -d [:space:]

luego en C puedes obtenerlo con:

FILE* stream = popen("system_profiler SPSoftwareDataType | grep /"Computer Name/" | cut -d: -f2 | tr -d [:space:]", "r"); ostringstream hoststream; while(!feof(stream) && !ferror(stream)) { char buf[128]; int byteRead = fread( buf, 1, 128, stream); hoststream.write(buf, byteRead); }


NSHost es lo que quieres aquí:

NSHost *host; host = [NSHost currentHost]; [host name];


C objetivo

El nombre que estaba buscando es:

[[NSHost currentHost] localizedName];

Devuelve "MacBook de Jonathan" en lugar de "Jonathans-Macbook", o "jonathans-macbook.local" que devuelve el name .

Swift 3

Para Swift> = 3 use.

if let deviceName = Host.current().localizedName { print(deviceName) }