-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceInfo.swift
124 lines (105 loc) · 3.34 KB
/
DeviceInfo.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright © 2024 Brian Drelling. All rights reserved.
import Foundation
import KippleFoundation
#if canImport(UIKit) && canImport(DeviceKit)
@_implementationOnly import DeviceKit
import UIKit
#endif
/// A contextualized representation of a device such as an iPhone, Mac, Apple Watch, and so on.
public struct DeviceInfo: Codable, Sendable, Equatable, Hashable {
// MARK: Properties
/// The family of the device (eg. "iPhone", "Apple TV").
public let deviceFamily: DeviceFamily
/// The model of the device (eg. "iPhone" or "iPod Touch").
public let deviceModel: String
/// The name of the operating system running on the device (eg. "iOS" or "tvOS").
public let systemName: String
/// The version of the operating system running on the device (eg. 10.5.1).
public let systemVersion: SemanticVersion
// MARK: Initializers
public init(
deviceFamily: DeviceFamily,
deviceModel: String,
systemName: String,
systemVersion: SemanticVersion
) {
self.deviceFamily = deviceFamily
self.deviceModel = deviceModel
self.systemName = systemName
self.systemVersion = systemVersion
}
}
// MARK: - Convenience
public extension DeviceInfo {
/// Information for the currently running device.
static let current: Self = .init(
deviceFamily: Self.currentDeviceFamily,
deviceModel: Self.currentDeviceModel,
systemName: Self.currentSystemName,
systemVersion: Self.currentSystemVersion
)
/// The family of the device (eg. "iPhone", "Apple TV").
private static var currentDeviceFamily: DeviceFamily {
#if os(iOS)
if Device.current.isPhone {
return .iPhone
} else if Device.current.isPad {
return .iPad
} else if Device.current.isPod {
return .iPod
} else {
return .unknown
}
#elseif os(watchOS)
return .appleWatch
#elseif os(tvOS)
return .appleTV
#elseif os(macOS)
return .mac
#else
return .unknown
#endif
}
/// The model of the device (eg. "iPhone" or "iPod Touch").
private static var currentDeviceModel: String {
#if canImport(UIKit)
Device.current.model ?? "Unknown"
#elseif os(macOS)
"Mac"
#else
"Unknown"
#endif
}
/// The name of the operating system running on the device (eg. "iOS" or "tvOS").
private static var currentSystemName: String {
#if canImport(UIKit)
Device.current.systemName ?? "Unknown"
#elseif os(macOS)
"macOS"
#elseif os(Linux)
"Linux"
#else
"Unknown"
#endif
}
/// The version of the operating system running on the device (eg. 10.5.1).
private static var currentSystemVersion: SemanticVersion {
#if canImport(UIKit)
.from(Device.current.systemVersion ?? "Unknown")
#elseif os(macOS) || os(Linux)
ProcessInfo.processInfo.operatingSystemVersion.semanticVersion
#else
"Unknown"
#endif
}
}
// MARK: - Extensions
private extension OperatingSystemVersion {
var semanticVersion: SemanticVersion {
.init(
major: self.majorVersion,
minor: self.minorVersion,
patch: self.patchVersion
)
}
}