Я использую sysctlbyname ("kern.hostname"), который не блокирует.Обратите внимание, что мой вспомогательный метод должен использоваться только для получения строковых атрибутов, а не целых чисел.
#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];
}