Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 获取我的Mac';计算机名_Macos_Cocoa - Fatal编程技术网

Macos 获取我的Mac';计算机名

Macos 获取我的Mac';计算机名,macos,cocoa,Macos,Cocoa,我怎样才能在Mac上获得计算机的名称?我所说的名称与您可以在“软件”下的“系统档案器”中找到的名称相同。就是您想要的名称: NSHost *host; host = [NSHost currentHost]; [host name]; 目标C 我要找的名字是: [[NSHost currentHost] localizedName]; 它返回“Jonathans MacBook”,而不是返回“Jonathans MacBook”或“Jonathans MacBook.local”,后者只返

我怎样才能在Mac上获得计算机的名称?我所说的名称与您可以在“软件”下的“系统档案器”中找到的名称相同。

就是您想要的名称:

NSHost *host;

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

目标C

我要找的名字是:

[[NSHost currentHost] localizedName];
它返回“Jonathans MacBook”,而不是返回“Jonathans MacBook”或“Jonathans MacBook.local”,后者只返回
name

Swift 3

对于Swift>=3使用

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

这里有一个不会阻塞的:

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

使用必须添加到项目中的系统配置.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];
#包括
...
//如果未设置计算机名或发生错误,则返回NULL/nil。OSX 10.1+
NSString*computerName=[(NSString*)SCDynamicStoreCopyComputerName(NULL,NULL)自动释放];
//如果未设置本地主机名或发生错误,则返回NULL/nil。OSX 10.2+
NSString*localHostname=[(NSString*)SCDynamicStoreCopyLocalHostName(NULL)自动删除];
我使用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];
}
#包括
-(NSString*)systemInfoString:(常量字符*)attributeName
{
大小;
sysctlbyname(attributeName,NULL,&size,NULL,0);//获取数据的大小。
char*attributeValue=malloc(大小);
int err=sysctlbyname(attributeName、attributeValue和size,NULL,0);
如果(错误!=0){
NSLog(@“sysctlbyname(%s)失败:%s”、attributeName、strerror(errno));
自由(属性值);
返回零;
}
NSString*vs=[NSString stringWithUTF8String:attributeValue];
自由(属性值);
回报vs;
}
-(NSString*)主机名
{
NSArray*components=[[self-systemInfoString:“kern.hostname”]componentsSeparatedByString:@.”;
返回[组件][0];
}

在您拥有的终端中:

system_profiler SPSoftwareDataType | grep "Computer Name" | cut -d: -f2 | tr -d [:space:]
然后在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);
  }

这将是一个用户友好的名称(包括空格和撇号),还是更简单的版本,其中空格被替换为-和“删除”。从链接文档:
可以是一个简单的主机名,如@“sales”,也可以是一个完全限定的域名,如@“sales.anycorp.com”。
不,我的意思是它会是“Jonathan's MacBook”或“Jonathans Macbook”?请注意[NSHost currentHost]执行阻塞网络查找。在慢速网络或断开连接的计算机上,除非您在后台线程上调用,否则它将停止您的应用程序,直到网络调用超时。注册服务时只需传递一个空字符串,您的本地化名称将自动使用,正如appleIs所建议的那样。有没有办法用Swift?dylan,是的,在swift 3中:
Host.current().localizedName
在swift it中显示错误为“使用未解析标识符'Host'”。请注意,苹果不建议在注册Bonjour服务时使用此方法。更多信息请参见。只是为了澄清,
ComputerName
LocalHostName
在这里是不同的。苹果公司建议不要使用LocalHostName,因为它的限制比Bonjour服务所需的更严格——Bonjour自动注册API使用ComputerName加上一些免费的重复数据消除魔法。最好的解决方案是AFAIK,而不是像
SCDynamicStoreCopyLocalHostName
,它将返回必需的“.local”后缀。我得到了
[:space::
的错误,所以我将最后一部分更改为
tr-d'
,然后它对我来说工作正常。