从WatchOS捕获Apple Watch的型号标识符 [英] Capture model identifier for Apple Watch from WatchOS
问题描述
似乎没有任何从手表应用程序获取Apple Watch模型的官方方法,但是这篇文章显示了 sysctlbyname
的特殊用法:
It looks like there aren't any documented official methods of obtaining the Apple Watch model from a watch app, but there is this post showing special use of sysctlbyname
:
有人可以帮助我弄清楚为什么此功能不起作用吗?有时会返回
Could anyone help me figure out why this function isn't working? It sometimes returns
"Watc \ t"
"Watc\t"
而不是预期的
"Watch2,4"
"Watch2,4"
Swift的sysctlbyname函数似乎是这里描述的c函数: https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/sysctlbyname.c.auto.html
Swift's sysctlbyname function seems to be this c function described here: https://opensource.apple.com/source/Libc/Libc-167/gen.subproj/sysctlbyname.c.auto.html
我的代码是从SO帖子的快速答案中复制的:
My code is copied from the swift answer in the SO post:
private func getWatchModel() -> String? {
var size: size_t = 0
sysctlbyname("hw.machine", nil, &size, nil, 0)
var machine = CChar()
sysctlbyname("hw.machine", &machine, &size, nil, 0)
return String(cString: &machine, encoding: String.Encoding.utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
} // Should return a string like "Watch2,4"
推荐答案
您永远不会分配缓冲区来接收机器字符串.更改
You never allocate a buffer to receive the machine string. Change
var machine = CChar()
到
var machine = [CChar](repeating: 0, count: size)
你应该很好!
这篇关于从WatchOS捕获Apple Watch的型号标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!