6

I'd like to find how to extract my computer name programmatically, similar to the output of following terminal command scutil --get ComputerName. This value can also be found under "About this mac" -> "System Software Overview" as shown in the following image :

enter image description here

So I've found one way is to use NSHost, but it's long been deprecated:

[[NSHost currentHost] localizedName]

In NSHost.h it mentioned where to look for ..

Use Network framework instead, see deprecation notice in <Foundation/NSHost.h>

So I've looked there and found nw_endpoint_get_hostname but it has no proper documentation for how to be used to fetch local hostname.

Any Idea how to fetch this data programmatically (swift/objective-c/c++)

Zohar81
  • 4,554
  • 5
  • 29
  • 82
  • 1
    I Actually saw this question, but they propose a way that has long been deprecated. – Zohar81 Nov 29 '21 at 21:50
  • 1
    Did you see any of the _other_ solutions in that post? I can see some Swift solutions in there that are not outdated... – Sweeper Nov 29 '21 at 21:56
  • Does the `uname()` C library routine not work? See `man 3 uname` – aMike Nov 29 '21 at 21:57
  • `ProcessInfo.processInfo.hostName` can be what you are looking for?! – marcusg Nov 29 '21 at 22:17
  • 1
    The key for me was `import SystemConfiguration` and `let name = SCDynamicStoreCopyComputerName(nil, nil)`. Here's the [documentation](https://developer.apple.com/documentation/systemconfiguration/1517208-scdynamicstorecopycomputername). – McKinley Jul 02 '22 at 14:37
  • If you use Swift, `Host.current().localizedName` is not deprecated and will return the computer name just as formatted in the System Software Overview. – daxmacrog Jul 08 '23 at 15:47

0 Answers0