Skip to content

Instantly share code, notes, and snippets.

@dphans
Last active September 2, 2021 12:23
Show Gist options
  • Save dphans/4c265667abd3c23a124cf9142fa4bc73 to your computer and use it in GitHub Desktop.
Save dphans/4c265667abd3c23a124cf9142fa4bc73 to your computer and use it in GitHub Desktop.
Determine if iOS app is running on a Mac, or other devices (iPad, iPhone, iWatch) programmatically
/// Swift version: 4
/// Last updated: Jul 21, 2020
// define somewhere... (utilities, constants,...)
enum TargetDevice {
case nativeMac
case iPad
case iPhone
case iWatch
public static var currentDevice: Self {
var currentDeviceModel = UIDevice.current.model
#if targetEnvironment(macCatalyst)
currentDeviceModel = "nativeMac"
#elseif os(watchOS)
currentDeviceModel = "watchOS"
#endif
if currentDeviceModel.starts(with: "iPhone") {
return .iPhone
}
if currentDeviceModel.starts(with: "iPad") {
return .iPad
}
if currentDeviceModel.starts(with: "watchOS") {
return .iWatch
}
return .nativeMac
}
}
// usage
print(TargetDevice.currentDevice)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment