如何确定Apple Watch型号?

时间:2018-03-03 17:53:41

标签: ios watchkit apple-watch

WKInterfaceDevice.current().model属性未提供型号:

  

对于Apple Watch,此字符串的值为 Apple Watch

如何从iOS确定Apple Watch模型?

Watch app screenshot, image credits https://support.apple.com/en-us/HT204507

3 个答案:

答案 0 :(得分:4)

没有公共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。

干杯。

答案 1 :(得分:3)

这是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"
    }
}

答案 2 :(得分:1)

只需略微更新WatchOS 5和Apple Watch Series 4的@Mathieu Vandeginste答案即可。

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)
}

对于WatchOS 5,需要进行一项更改以从getWatchModel()修剪原始字符串,因为它现在在使用以下标签的制表符中结束:?.trimmingCharacters(in: .whitespacesAndNewlines)

private func getWatchName(model: String) -> String {
    switch model {
    case "Watch1,1":
        return "Apple Watch 38mm"
    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"
    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"
    default:
        return "unknown"
    }
}

只需根据this article

在上面添加新模型