I am trying to port one of my iOS applications to Mac OS X, and I am struggling to find the UIDevice
-like object for OS X. I am interested in getting the name of the device, such as "MacBookAir".
EDIT/ANSWER
As Josh Caswell pointed out, you can use SCDynamicStoreCopyComputerName
key.
Here is the code:
+ (NSString *)computerName {
return [(id)SCDynamicStoreCopyComputerName(NULL, NULL) autorelease];
}
The Net Services Programming Guide says:
In Mac OS X on the desktop, calling the
SCDynamicStore
function from the System Configuration framework returns the computer name so you can manipulate it like any other string. In iOS, you can obtain the same information from the name property of theUIDevice
class.
There doesn't seem to be a single function called SCDynamicStore
(doc bug), but you probably want SCDynamicStoreCopyValue
. Looks like you get the key you need using SCDynamicStoreKeyCreateComputerName
.
EDIT: Or, even better, as you found, use the SCDynamicStoreCopyComputerName
function, found in SCDynamicStoreCopySpecific.h
Swift 4.0 OSX
Here is what I needed for a similar value to UIDevice name
iOS (swift 3.2)
let deviceName = UIDevice.current.name
OSX (swift 4.0)
let deviceName = Host.current().name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With