问题
The WKInterfaceDevice.current().model
property does not give a model number:
For Apple Watch, the value of this string is Apple Watch.
How can the exact Apple Watch model be determined from iOS?
回答1:
There is no public API to get that exact information.
You can however use the following (I'll let you translate into 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;
}
This returns a string in the format: "Watch1,1". You'll need to provide a lookup table to do ID -> Name translation.
"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
By the way, this sysctlbyname
API also works for iOS.
Cheers.
回答2:
EDIT: no longer works for watchOS 4+
This is the Swift 4+ version: working fine for me :)
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:
Just slightly updating @Mathieu Vandeginste answer for WatchOS 6 and Apple Watch Series 5.
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)
}
This needed one change for WatchOS 5 to trim the raw string from getWatchModel()
as it now ends in a tab using: ?.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"
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"
default:
return "unknown"
}
}
Just added the new models to the above based on this article
来源:https://stackoverflow.com/questions/49087330/how-to-determine-the-apple-watch-model