简体   繁体   中英

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];
}

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

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
NSHost *host = [NSHost currentHost];
NSLog(@"hostName %@",[host localizedName]);

you can see the mac name

With the deprecation of Host , you might consider ProcessInfo.hostName as an alternative. In my case it returned:

ExampleHostName.local

You can try NSHost. Heres the class documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM