[英]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".我正在尝试将我的 iOS 应用程序之一移植到 Mac OS X,并且我正在努力为 OS X 找到类似于
UIDevice
的 object。我有兴趣获取设备的名称,例如“MacBookAir”。
EDIT/ANSWER编辑/回答
As Josh Caswell pointed out, you can use SCDynamicStoreCopyComputerName
key.正如 Josh Caswell 指出的,您可以使用
SCDynamicStoreCopyComputerName
键。
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.在桌面上的Mac OS X中,从系统配置框架调用
SCDynamicStore
函数会返回计算机名称,以便您可以像处理任何其他字符串一样对其进行操作。 In iOS, you can obtain the same information from the name property of theUIDevice
class.在iOS中,您可以从
UIDevice
类的name属性获取相同的信息。
There doesn't seem to be a single function called SCDynamicStore
(doc bug), but you probably want SCDynamicStoreCopyValue
. 似乎没有一个名为
SCDynamicStore
函数(doc bug),但您可能需要SCDynamicStoreCopyValue
。 Looks like you get the key you need using SCDynamicStoreKeyCreateComputerName
. 看起来您使用
SCDynamicStoreKeyCreateComputerName
获得了所需的密钥。
EDIT: Or, even better, as you found, use the SCDynamicStoreCopyComputerName
function, found in SCDynamicStoreCopySpecific.h 编辑:或者,甚至更好,正如您所发现的,使用
SCDynamicStoreCopyComputerName
函数。
Swift 4.0 OSX Swift 4.0 OSX
Here is what I needed for a similar value to UIDevice name 这是我对UIDevice名称的类似值所需要的
iOS (swift 3.2) iOS(swift 3.2)
let deviceName = UIDevice.current.name
OSX (swift 4.0) OSX(swift 4.0)
let deviceName = Host.current().name
NSHost *host = [NSHost currentHost];
NSLog(@"hostName %@",[host localizedName]);
you can see the mac name 你可以看到mac名称
With the deprecation of Host
, you might consider ProcessInfo.hostName
as an alternative.随着
Host
的弃用,您可能会考虑ProcessInfo.hostName
作为替代方案。 In my case it returned:就我而言,它返回了:
ExampleHostName.local
You can try NSHost. 你可以尝试NSHost。 Heres the class documentation
下面是课程文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.