Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get my Mac's computer name

Tags:

macos

cocoa

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".

like image 417
Jonathan. Avatar asked Oct 31 '10 12:10

Jonathan.


People also ask

Do Macs have computer names?

Your computer's name is displayed at the top of the Sharing preferences pane. On your Mac, choose Apple menu > System Preferences, then click Sharing . If the lock at the bottom left is locked , click it to unlock the preference pane. Type a new name in the Computer Name field.


1 Answers

Objective 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.

Swift 3

For Swift >= 3 use.

if let deviceName = Host.current().localizedName {    print(deviceName) } 
like image 65
Jonathan. Avatar answered Sep 23 '22 07:09

Jonathan.