ios 如何确定Apple Watch型号?

czq61nw1  于 2023-03-20  发布在  iOS
关注(0)|答案(4)|浏览(402)

WKInterfaceDevice.current().model属性不给予型号:
对于Apple Watch,此字符串的值为 Apple Watch
如何从iOS中确定Apple Watch的确切型号?

cxfofazt

cxfofazt1#

只是稍微更新了WatchOS 6和苹果手表系列5的@Mathieu Vandeginste答案。

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

刚刚在this article的基础上添加了新型号

5uzkadbs

5uzkadbs2#

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

这将返回以下格式的字符串:“Watch 1,1”。您需要提供一个查找表来进行ID -〉Name转换。

"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。
干杯。

mkshixfv

mkshixfv3#

编辑:不再适用于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"
    }
}
7dl7o3gd

7dl7o3gd4#

针对watchOS 8和Apple Watch系列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"
    }
}

相关问题