macos 获取我的 Mac 的计算机名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4063129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get my Mac's computer name
提问by Jonathan.
How can I get the computer's name on a Mac? I'm talking about the same name as the one you can find in System Profiler under "Software".
如何在 Mac 上获取计算机的名称?我说的是与您可以在 System Profiler 中“软件”下找到的名称相同的名称。
回答by Jonathan.
Objective C
目标C
The name I was looking for is:
我要找的名字是:
[[NSHost currentHost] localizedName];
It returns "Jonathan's MacBook" rather than "Jonathans-Macbook", or "jonathans-macbook.local" which just name
returns.
它返回“Jonathan's MacBook”而不是“Jonathans-Macbook”,或“jonathans-macbook.local”,后者只是name
返回。
Swift 3
斯威夫特 3
For Swift >= 3 use.
对于 Swift >= 3 使用。
if let deviceName = Host.current().localizedName {
print(deviceName)
}
回答by ennuikiller
回答by Mark de Rooi
I use sysctlbyname("kern.hostname"), which does not block. Please note that my helper method should only be used to retrieve string attributes, not integers.
我使用 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];
}
回答by Dave Gallagher
Using the SystemConfiguration.framework, which you must add to your project:
使用SystemConfiguration.framework,您必须将其添加到您的项目中:
#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];
回答by antoine
in terminal you have it with :
在终端你有它:
system_profiler SPSoftwareDataType | grep "Computer Name" | cut -d: -f2 | tr -d [:space:]
then in C you can get it with :
然后在 C 中你可以得到它:
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);
}
回答by J. Perkins
Here's one that doesn't block:
这是一个不会阻塞的:
NSString* name = [(NSString*)CSCopyMachineName() autorelease];