Skip to content

Instantly share code, notes, and snippets.

@markbattistella
Created February 19, 2024 23:27
Show Gist options
  • Save markbattistella/527a421536f18d2c19456b19a4c1284b to your computer and use it in GitHub Desktop.
Save markbattistella/527a421536f18d2c19456b19a4c1284b to your computer and use it in GitHub Desktop.
A Swift Utility for OS Environment Checks
//
// Author: Mark Battistella
// Website: https://markbattistella.com
//
#if os(iOS)
import UIKit
#else
import Foundation
#endif
/// PlatformCheck provides a collection of static properties to determine the current operating system
/// or environment the code is running in. This utility is useful for conditional compilation and runtime
/// decisions in cross-platform Swift projects.
///
/// Usage of an enum without cases emphasizes that `PlatformCheck` is not meant to be instantiated,
/// but rather serves as a namespace for these static utilities.
enum PlatformCheck {
/// Indicates if the current target is iOS, excluding Mac Catalyst.
static var isiOS: Bool {
#if os(iOS) && !targetEnvironment(macCatalyst)
return UIDevice.current.userInterfaceIdiom == .phone
#else
return false
#endif
}
/// Indicates if the current device is an iPad, excluding Mac Catalyst.
static var isiPad: Bool {
#if os(iOS) && !targetEnvironment(macCatalyst)
return UIDevice.current.userInterfaceIdiom == .pad
#else
return false
#endif
}
/// Indicates if the current target is tvOS.
static var isTVOS: Bool {
#if os(tvOS)
return true
#else
return false
#endif
}
/// Indicates if the current target is macOS, excluding Mac Catalyst apps.
static var isMac: Bool {
#if os(macOS) && !targetEnvironment(macCatalyst)
return true
#else
return false
#endif
}
/// Indicates if the app is running as a Mac Catalyst app.
static var isMacCatalyst: Bool {
#if targetEnvironment(macCatalyst)
return true
#else
return false
#endif
}
/// Indicates if the current target is watchOS.
static var isWatchOS: Bool {
#if os(watchOS)
return true
#else
return false
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment