Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDevice class for Mac OS X?

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];
}
like image 983
Cyprian Avatar asked Nov 01 '11 21:11

Cyprian


2 Answers

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 the UIDevice 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

like image 161
jscs Avatar answered Nov 13 '22 06:11

jscs


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
like image 30
Jacksonsox Avatar answered Nov 13 '22 05:11

Jacksonsox