Created
May 15, 2023 12:42
-
-
Save davidsteppenbeck/0ea7b66172056869610932d2cd138cc3 to your computer and use it in GitHub Desktop.
Typealias + extension examples for multiplatform development in Swift.
This file contains hidden or 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
import SwiftUI | |
/* | |
Put these in a file that is compiled for iOS only. | |
Note that these are just a few examples of things that worked well in my project. | |
They may or may not be useful to you, but they give an idea of how typealias + extensions | |
could be used to keep your code tidy. | |
*/ | |
typealias UserInterfaceStyle = UIUserInterfaceStyle | |
extension Color { | |
/// The color for the main background of your interface. | |
static let systemBackground = Color(.systemBackground) | |
/// The color for content layered on top of the main background. | |
static let secondarySystemBackground = Color(.secondarySystemBackground) | |
} | |
typealias Device = UIDevice | |
extension UIDevice { | |
/// Provides the operating system name, for example, iOS, iPadOS, or macOS. | |
var multiplatformSystemName: String { | |
switch userInterfaceIdiom { | |
case .pad: | |
// On iPad, the `systemName` property returns "iOS". | |
return systemName.replacingOccurrences(of: "iOS", with: "iPadOS") | |
default: | |
return systemName | |
} | |
} | |
} |
This file contains hidden or 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
import SwiftUI | |
/* | |
Put these in a file that is compiled for macOS only. | |
Note that these are just a few examples of things that worked well in my project. | |
They may or may not be useful to you, but they give an idea of how typealias + extensions | |
could be used to keep your code tidy. | |
*/ | |
typealias UIApplication = NSApplication | |
extension NSApplication { | |
/// Always returns `true` on macOS. | |
var supportsAlternateIcons: Bool { true } | |
/// Changes the icon the system displays for the app. | |
/// | |
/// The error passed to the completion handler is always `nil` on macOS. | |
func setAlternateIconName(_ alternateIconName: String?, completionHandler: ((Error?) -> Void)? = nil) { | |
if let alternateIconName { | |
NSApplication.shared.applicationIconImage = NSImage(named: alternateIconName) | |
UserDefaults.alternateIconName = alternateIconName | |
} else { | |
NSApplication.shared.applicationIconImage = nil | |
UserDefaults.resetAlternateIconName() | |
} | |
completionHandler?(nil) | |
} | |
} | |
typealias UIAccessibility = NSAccessibility | |
extension NSAccessibility { | |
/// Always returns `false` on macOS. | |
static var isReduceMotionEnabled: Bool { false } | |
} | |
typealias UIColor = NSColor | |
extension Color { | |
/// The color for the main background of the interface. | |
static let systemBackground = Color("SystemBackgroundMacOS") | |
/// The color for content layered on top of the main background. | |
static let secondarySystemBackground = Color("SecondarySystemBackgroundMacOS") | |
} | |
enum UserInterfaceStyle: String, CaseIterable { | |
/// The light interface style. | |
case light | |
/// The dark interface style. | |
case dark | |
/// An unspecified interface style. | |
/// | |
/// Choose this option when you want to follow the system's interface style. | |
case unspecified | |
} | |
extension UserInterfaceStyle: LocalizedTitleConvertible { | |
// Documentation inherited from protocol. | |
var localizedTitle: String { | |
switch self { | |
case .light: | |
return String(localized: "USER_INTERFACE_STYLE_CASE_LIGHT", comment: "Light") | |
case .dark: | |
return String(localized: "USER_INTERFACE_STYLE_CASE_DARK", comment: "Dark") | |
case .unspecified: | |
return String(localized: "USER_INTERFACE_STYLE_CASE_UNSPECIFIED_SHORT", comment: "System") | |
} | |
} | |
} | |
extension UserInterfaceStyle: Identifiable { | |
// Documentation inherited from protocol. | |
var id: Self { self } | |
} | |
struct Device { | |
/// Represents the current device. | |
static let current = Device() | |
/// The model of the device. | |
/// | |
/// Returns "Mac" on macOS. | |
var model: String { "Mac" } | |
/// The name of the device. | |
/// | |
/// Provides the full user name on macOS, for example, "Bob McBobface". | |
var name: String { | |
ProcessInfo.processInfo.fullUserName | |
} | |
/// Provides the operating system name, for example, iOS, iPadOS, or macOS. | |
/// | |
/// Returns "macOS" on macOS. | |
var multiplatformSystemName: String { "macOS" } | |
/// Provides system OS version, for example, 13.2. | |
var systemVersion: String { | |
let majorMinorVersionString = "\(ProcessInfo.processInfo.operatingSystemVersion.majorVersion).\(ProcessInfo.processInfo.operatingSystemVersion.minorVersion)" | |
let patchVersion = ProcessInfo.processInfo.operatingSystemVersion.patchVersion | |
if patchVersion > 0 { | |
return majorMinorVersionString + ".\(patchVersion)" | |
} else { | |
return majorMinorVersionString | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment