Last active
September 2, 2021 12:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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