We know that there are two screen sizes for Apple Watch: 38mm and 42mm. The WKInterfaceDevice
class provides a readable property named screenBounds
. I wrote an extension for WKInterfaceDevice
, trying to add a method to detect current device type.
import WatchKit
enum WatchResolution {
case Watch38mm, Watch42mm
}
extension WKInterfaceDevice {
class func currentResolution() -> WatchResolution {
let watch38mmRect = CGRectMake(0.0, 0.0, 136.0, 170.0)
let watch42mmRect = CGRectMake(0.0, 0.0, 156.0, 195.0)
let currentBounds = WKInterfaceDevice.currentDevice().screenBounds
if CGRectEqualToRect(currentBounds, watch38mmRect) {
return WatchResolution.Watch38mm
} else {
return WatchResolution.Watch42mm
}
}
}
Is that the correct method to detect Apple Watch size? Is there another method I am missing in the Apple docs?
Tap the My Watch tab and then tap General. Tap About. On this screen, you'll see a section that says Model. It will contain a number in it, but you'll need to tap this number to reveal the model number.
Both version offer retina resolutions, but the 42mm packs more pixels with 312 x 390 pixels as compared to the 272 x 340 resolution of the 38mm model. If you want a bigger display and more pixels, then you should purchase the 42mm model.
Your code looks good, but has a few minor issues:
CGRectMake
but in Swift you should use a CGRect
initializerCGRectEqualToRect
but in Swift you can just use ==
or switch
WatchResolution
enums, but you don't need to be explicit - Swift will figure it out from your method signaturewatch42mmRect
but not using it for anythingI would rewrite it like this:
enum WatchResolution {
case Watch38mm, Watch42mm, Unknown
}
extension WKInterfaceDevice {
class func currentResolution() -> WatchResolution {
let watch38mmRect = CGRect(x: 0, y: 0, width: 136, height: 170)
let watch42mmRect = CGRect(x: 0, y: 0, width: 156, height: 195)
let currentBounds = WKInterfaceDevice.currentDevice().screenBounds
switch currentBounds {
case watch38mmRect:
return .Watch38mm
case watch42mmRect:
return .Watch42mm
default:
return .Unknown
}
}
}
Update Swift 4:
It includes new launch of Watch resolutions:
enum WatchResolution {
case Watch38mm, Watch40mm,Watch42mm,Watch44mm, Unknown
}
extension WKInterfaceDevice {
class func currentResolution() -> WatchResolution {
let watch38mmRect = CGRect(x: 0, y: 0, width: 136, height: 170)
let watch40mmRect = CGRect(x: 0, y: 0, width: 162, height: 197)
let watch42mmRect = CGRect(x: 0, y: 0, width: 156, height: 195)
let watch44mmRect = CGRect(x: 0, y: 0, width: 184, height: 224)
let currentBounds = WKInterfaceDevice.current().screenBounds
switch currentBounds {
case watch38mmRect:
return .Watch38mm
case watch40mmRect:
return .Watch40mm
case watch42mmRect:
return .Watch42mm
case watch44mmRect:
return .Watch44mm
default:
return .Unknown
}
}
}
Usage
let resol = WKInterfaceDevice.currentResolution()
switch resol {
case .Watch38mm, .Watch42mm:
// Do Something
case .Watch40mm, .Watch44mm:
// Do Something
default:
// Do Something
}
Reference Link: Apple Developer Watch Interface Link
Hope that helps....
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With