如何判断Apple Watch型号?

问题描述 投票:0回答:5

WKInterfaceDevice.current().model
属性不给型号:

对于Apple Watch,这个字符串的值为Apple Watch

如何从 iOS 确定确切的 Apple Watch 型号?

ios watchkit apple-watch
5个回答
10
投票

只是稍微更新了 @Mathieu Vandeginste 对 WatchOS 6 和 Apple Watch Series 5 的回答。

private func getWatchModel() -> String? {
    var size: size_t = 0
    sysctlbyname("hw.machine", nil, &size, nil, 0)
    var machine = [CChar](repeating: 0, count: size)
    sysctlbyname("hw.machine", &machine, &size, nil, 0)
    return String(cString: &machine, encoding: String.Encoding.utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
}

这需要对 WatchOS 5 进行更改以从

getWatchModel()
修剪原始字符串,因为它现在以选项卡结尾,使用:
?.trimmingCharacters(in: .whitespacesAndNewlines)

private func getWatchName(model: String) -> String {
    switch model {
        case "Watch1,1":
            return "Apple Watch Series 0 38mm"
        case "Watch1,2":
            return"Apple Watch Series 0 42mm"
        case "Watch2,3":
            return "Apple Watch Series 2 38mm"
        case "Watch2,4":
            return "Apple Watch Series 2 42mmm"
        case "Watch2,6":
            return "Apple Watch Series 1 38mm"
        case "Watch2,7":
            return "Apple Watch Series 1 42mm"
        case "Watch3,1":
            return "Apple Watch Series 3 38mm Cellular"
        case "Watch3,2":
            return "Apple Watch Series 3 42mm Cellular"
        case "Watch3,3":
            return "Apple Watch Series 3 38mm"
        case "Watch3,4":
            return "Apple Watch Series 3 42mm"
        case "Watch4,1":
            return "Apple Watch Series 4 40mm"
        case "Watch4,2":
            return "Apple Watch Series 4 44mm"
        case "Watch4,3":
            return "Apple Watch Series 4 40mm Cellular"
        case "Watch4,4":
            return "Apple Watch Series 4 44mm Cellular"
        case "Watch5,1":
            return "Apple Watch Series 5 40mm"
        case "Watch5,2":
            return "Apple Watch Series 5 44mm"
        case "Watch5,3":
            return "Apple Watch Series 5 40mm Cellular"
        case "Watch5,4":
            return "Apple Watch Series 5 44mm Cellular"
        case "Watch5,9":
            return "Apple Watch SE 40mm"
        case "Watch5,10":
            return "Apple Watch SE 44mm"
        case "Watch5,11":
            return "Apple Watch SE 40mm Cellular"
        case "Watch5,12":
            return "Apple Watch SE 44mm Cellular"
        case "Watch6,1":
            return "Apple Watch Series 6 40mm"
        case "Watch6,2":
            return "Apple Watch Series 6 44mm"
        case "Watch6.3":
            return "Apple Watch Series 6 40mm Cellular"
        case "Watch6,4":
            return "Apple Watch Series 6 44mm Cellular"
        case "Watch6,6":
            return "Apple Watch Series 7 41mm"
        case "Watch6,7":
            return "Apple Watch Series 7 45mm"
        case "Watch6,8":
            return "Apple Watch Series 7 41mm Cellular"
        case "Watch6,9":
            return "Apple Watch Series 7 45mm Cellular"
        default:
            return "unknown"
    }
}

刚刚在这篇文章的基础上添加了新模型


5
投票

没有公共 API 来获取准确的信息。

但是您可以使用以下内容(我会让您翻译成 Swift):

- (NSString*) modelIdentifier {
    size_t size = 0;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char* machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString* model = [NSString stringWithCString: machine encoding: NSUTF8StringEncoding];
    free(machine);
    return model;
}

这将返回格式为“Watch1,1”的字符串。您需要提供一个查找表来执行 ID -> 名称翻译。

"Watch1,1" -> Apple Watch 38mm
"Watch1,2" -> Apple Watch 42mm
"Watch2,3" -> Apple Watch Series 2 38mm
"Watch2,4" -> Apple Watch Series 2 42mm
"Watch2,6" -> Apple Watch Series 1 38mm
"Watch2,7" -> Apple Watch Series 1 42mm
"Watch3,1" -> Apple Watch Series 3 38mm Cellular
"Watch3,2" -> Apple Watch Series 3 42mm Cellular
"Watch3,3" -> Apple Watch Series 3 38mm
"Watch3,4" -> Apple Watch Series 3 42mm

顺便说一下,这个

sysctlbyname
API 也适用于 iOS。

干杯。


5
投票

编辑:不再适用于 watchOS 4+

这是 Swift 4+ 版本:对我来说工作正常:)

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)
    let model = String(cString: &machine, encoding: String.Encoding.utf8)
    switch model {
    case "Watch1,1":
        return "Apple Watch 28mm"
    case "Watch1,2":
        return"Apple Watch 42mm"
    case "Watch2,3":
        return "Apple Watch Series 2 38mm"
    case "Watch2,4":
        return "Apple Watch Series 2 42mmm"
    case "Watch2,6":
        return "Apple Watch Series 1 38mm"
    case "Watch2,7":
        return "Apple Watch Series 1 42mm"
    case "Watch3,1":
        return "Apple Watch Series 3 38mm Cellular"
    case "Watch3,2":
        return "Apple Watch Series 3 42mm Cellular"
    case "Watch3,3":
        return "Apple Watch Series 3 38mm"
    case "Watch3,4":
        return "Apple Watch Series 3 42mm"
    default:
        return "unknown"
    }
}

3
投票

已针对 watchOS 8 和 Apple Watch Series 7 进行更新

其他答案说这不起作用,因为函数返回

Watc\t
,这解决了这个问题。

public func getWatchModel() -> String? {
    var size: size_t = 0
    sysctlbyname("hw.machine", nil, &size, nil, 0)
    var machine = [CChar](repeating: 0, count: size)
    sysctlbyname("hw.machine", &machine, &size, nil, 0)
    return String(cString: &machine, encoding: String.Encoding.utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
}

public func getWatchName(model: String) -> String {
    switch model {
    case "Watch1,1":
        return "Apple Watch Series 0 38mm"
    case "Watch1,2":
        return"Apple Watch Series 0 42mm"
    case "Watch2,3":
        return "Apple Watch Series 2 38mm"
    case "Watch2,4":
        return "Apple Watch Series 2 42mmm"
    case "Watch2,6":
        return "Apple Watch Series 1 38mm"
    case "Watch2,7":
        return "Apple Watch Series 1 42mm"
    case "Watch3,1":
        return "Apple Watch Series 3 38mm Cellular"
    case "Watch3,2":
        return "Apple Watch Series 3 42mm Cellular"
    case "Watch3,3":
        return "Apple Watch Series 3 38mm"
    case "Watch3,4":
        return "Apple Watch Series 3 42mm"
    case "Watch4,1":
        return "Apple Watch Series 4 40mm"
    case "Watch4,2":
        return "Apple Watch Series 4 44mm"
    case "Watch4,3":
        return "Apple Watch Series 4 40mm Cellular"
    case "Watch4,4":
        return "Apple Watch Series 4 44mm Cellular"
    case "Watch5,1":
        return "Apple Watch Series 5 40mm"
    case "Watch5,2":
        return "Apple Watch Series 5 44mm"
    case "Watch5,3":
        return "Apple Watch Series 5 40mm Cellular"
    case "Watch5,4":
        return "Apple Watch Series 5 44mm Cellular"
    case "Watch5,9":
        return "Apple Watch SE 40mm"
    case "Watch5,10":
        return "Apple Watch SE 44mm"
    case "Watch5,11":
        return "Apple Watch SE 40mm Cellular"
    case "Watch5,12":
        return "Apple Watch SE 44mm Cellular"
    case "Watch6,1":
        return "Apple Watch Series 6 40mm"
    case "Watch6,2":
        return "Apple Watch Series 6 44mm"
    case "Watch6.3":
        return "Apple Watch Series 6 40mm Cellular"
    case "Watch6,4":
        return "Apple Watch Series 6 44mm Cellular"
    case "Watch6,6":
        return "Apple Watch Series 7 41mm"
    case "Watch6,7":
        return "Apple Watch Series 7 45mm"
    case "Watch6,8":
        return "Apple Watch Series 7 41mm Cellular"
    case "Watch6,9":
        return "Apple Watch Series 7 45mm Cellular"
    case "x86_64":
        return "Apple Watch Series 9 Simulator"
    default:
        return "unknown"
    }
}

-2
投票

你的问题的答案在这里

This answer supports all series up to the Series Ultra

https://gist.github.com/sharaev-vl/c6b01bfb3238330acfdda50ad50a1f3c

© www.soinside.com 2019 - 2024. All rights reserved.