1 |
efrain |
1 |
<img src="/DeviceKit.svg" height="128">
|
|
|
2 |
|
|
|
3 |
[](https://raw.githubusercontent.com/devicekit/DeviceKit/master/LICENSE)
|
|
|
4 |
[](https://cocoapods.org/pods/DeviceKit)
|
|
|
5 |
[](https://github.com/Carthage/Carthage)
|
|
|
6 |
[](https://codecov.io/gh/devicekit/DeviceKit)
|
|
|
7 |
[](https://cocoapods.org/pods/DeviceKit)
|
|
|
8 |
[](https://codeclimate.com/github/devicekit/DeviceKit/maintainability)
|
|
|
9 |
[](http://cocoadocs.org/docsets/DeviceKit)
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
| Branch | Build Status | Versions |
|
|
|
13 |
|:---------|:--------------:|:----------:|
|
|
|
14 |
| **master** |[](https://travis-ci.org/devicekit/DeviceKit)| ≥ 2.0 |
|
|
|
15 |
| **Swift 4 - 4.2** |[](https://travis-ci.org/devicekit/DeviceKit)| ≥ 1.3 < 1.13 |
|
|
|
16 |
| **Swift 3** |[](https://travis-ci.org/devicekit/DeviceKit)| ≥ 1.0 < 1.3 |
|
|
|
17 |
| **Swift 2.3** |[](https://travis-ci.org/devicekit/DeviceKit)| < 1.0 |
|
|
|
18 |
|
|
|
19 |
`DeviceKit` is a value-type replacement of [`UIDevice`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/).
|
|
|
20 |
|
|
|
21 |
## Current version 4.6.0.
|
|
|
22 |
See our detailed [changelog](CHANGELOG.md) for the latest features, improvements and bug fixes.
|
|
|
23 |
|
|
|
24 |
## Features
|
|
|
25 |
|
|
|
26 |
- [x] Equatable
|
|
|
27 |
- [x] Device identification
|
|
|
28 |
- [x] Device family detection
|
|
|
29 |
- [x] Device group detection
|
|
|
30 |
- [x] Simulator detection
|
|
|
31 |
- [x] Battery state
|
|
|
32 |
- [x] Battery level
|
|
|
33 |
- [x] Various device metrics (e.g. screen size, screen ratio, PPI)
|
|
|
34 |
- [x] Low Power Mode detection
|
|
|
35 |
- [x] Guided Access Session detection
|
|
|
36 |
- [x] Screen brightness
|
|
|
37 |
- [x] Display Zoom detection
|
|
|
38 |
- [x] Detect available sensors (Touch ID, Face ID)
|
|
|
39 |
- [x] Detect available disk space
|
|
|
40 |
- [x] Apple Pencil support detection
|
|
|
41 |
|
|
|
42 |
## Requirements
|
|
|
43 |
|
|
|
44 |
- iOS 9.0+ (linking against iOS 9.3 required)
|
|
|
45 |
- tvOS 9.0+ (linking against tvOS 9.2 required)
|
|
|
46 |
- watchOS 2.0+
|
|
|
47 |
|
|
|
48 |
## Installation
|
|
|
49 |
DeviceKit can be installed in various ways.
|
|
|
50 |
|
|
|
51 |
### CocoaPods
|
|
|
52 |
|
|
|
53 |
#### Swift 5
|
|
|
54 |
```ruby
|
|
|
55 |
pod 'DeviceKit', '~> 4.0'
|
|
|
56 |
```
|
|
|
57 |
#### iOS 8.0 support
|
|
|
58 |
```ruby
|
|
|
59 |
pod 'DeviceKit', '3.2'
|
|
|
60 |
```
|
|
|
61 |
#### Swift 4.0 - Swift 4.2
|
|
|
62 |
```ruby
|
|
|
63 |
pod 'DeviceKit', '~> 1.3'
|
|
|
64 |
```
|
|
|
65 |
#### Swift 3
|
|
|
66 |
```ruby
|
|
|
67 |
pod 'DeviceKit', '~> 1.2.3'
|
|
|
68 |
```
|
|
|
69 |
#### Swift 2.3 (Unsupported)
|
|
|
70 |
```ruby
|
|
|
71 |
pod 'DeviceKit', :git => 'https://github.com/devicekit/DeviceKit.git', :branch => 'swift-2.3-unsupported'
|
|
|
72 |
```
|
|
|
73 |
|
|
|
74 |
### Swift Package Manager
|
|
|
75 |
|
|
|
76 |
#### Swift 5
|
|
|
77 |
```swift
|
|
|
78 |
dependencies: [
|
|
|
79 |
.package(url: "https://github.com/devicekit/DeviceKit.git", from: "4.0.0"),
|
|
|
80 |
/// ...
|
|
|
81 |
]
|
|
|
82 |
```
|
|
|
83 |
#### iOS 8.0 support
|
|
|
84 |
```swift
|
|
|
85 |
dependencies: [
|
|
|
86 |
.package(url: "https://github.com/devicekit/DeviceKit.git", from: "3.2.0"),
|
|
|
87 |
/// ...
|
|
|
88 |
]
|
|
|
89 |
```
|
|
|
90 |
|
|
|
91 |
### Carthage
|
|
|
92 |
|
|
|
93 |
#### Swift 5
|
|
|
94 |
```ogdl
|
|
|
95 |
github "devicekit/DeviceKit" ~> 4.0
|
|
|
96 |
```
|
|
|
97 |
#### iOS 8.0 support
|
|
|
98 |
```ogdl
|
|
|
99 |
github "devicekit/DeviceKit" ~> 3.2
|
|
|
100 |
```
|
|
|
101 |
#### Swift 4.0 - Swift 4.2
|
|
|
102 |
```ogdl
|
|
|
103 |
github "devicekit/DeviceKit" ~> 1.3
|
|
|
104 |
```
|
|
|
105 |
#### Swift 3
|
|
|
106 |
```ogdl
|
|
|
107 |
github "devicekit/DeviceKit" ~> 1.2.3
|
|
|
108 |
```
|
|
|
109 |
#### Swift 2.3 (Unsupported)
|
|
|
110 |
```ogdl
|
|
|
111 |
github "devicekit/DeviceKit" "swift-2.3-unsupported"
|
|
|
112 |
```
|
|
|
113 |
|
|
|
114 |
### Manually
|
|
|
115 |
To install it manually, drag the `DeviceKit` project into your app project in Xcode. Or add it as a git submodule by running:
|
|
|
116 |
```bash
|
|
|
117 |
$ git submodule add https://github.com/devicekit/DeviceKit.git
|
|
|
118 |
```
|
|
|
119 |
|
|
|
120 |
## Usage
|
|
|
121 |
First make sure to import the framework:
|
|
|
122 |
```swift
|
|
|
123 |
import DeviceKit
|
|
|
124 |
```
|
|
|
125 |
|
|
|
126 |
Here are some usage examples. All devices are also available as simulators:
|
|
|
127 |
```swift
|
|
|
128 |
.iPhone6 => .simulator(.iPhone6)
|
|
|
129 |
.iPhone6s => .simulator(.iPhone6s)
|
|
|
130 |
```
|
|
|
131 |
|
|
|
132 |
You can try these examples in Playground.
|
|
|
133 |
|
|
|
134 |
**Note:**
|
|
|
135 |
|
|
|
136 |
> To try DeviceKit in the playground, open the `DeviceKit.xcworkspace` and build DeviceKit.framework for any simulator first by selecting "DeviceKit" as your current scheme.
|
|
|
137 |
|
|
|
138 |
### Get the Device You're Running On
|
|
|
139 |
```swift
|
|
|
140 |
let device = Device.current
|
|
|
141 |
|
|
|
142 |
print(device) // prints, for example, "iPhone 6 Plus"
|
|
|
143 |
|
|
|
144 |
if device == .iPhone6Plus {
|
|
|
145 |
// Do something
|
|
|
146 |
} else {
|
|
|
147 |
// Do something else
|
|
|
148 |
}
|
|
|
149 |
```
|
|
|
150 |
|
|
|
151 |
### Get the Device Family
|
|
|
152 |
```swift
|
|
|
153 |
let device = Device.current
|
|
|
154 |
if device.isPod {
|
|
|
155 |
// iPods (real or simulator)
|
|
|
156 |
} else if device.isPhone {
|
|
|
157 |
// iPhone (real or simulator)
|
|
|
158 |
} else if device.isPad {
|
|
|
159 |
// iPad (real or simulator)
|
|
|
160 |
}
|
|
|
161 |
```
|
|
|
162 |
|
|
|
163 |
### Check If Running on Simulator
|
|
|
164 |
```swift
|
|
|
165 |
let device = Device.current
|
|
|
166 |
if device.isSimulator {
|
|
|
167 |
// Running on one of the simulators(iPod/iPhone/iPad)
|
|
|
168 |
// Skip doing something irrelevant for Simulator
|
|
|
169 |
}
|
|
|
170 |
```
|
|
|
171 |
|
|
|
172 |
### Get the Simulator Device
|
|
|
173 |
```swift
|
|
|
174 |
let device = Device.current
|
|
|
175 |
switch device {
|
|
|
176 |
case .simulator(.iPhone6s): break // You're running on the iPhone 6s simulator
|
|
|
177 |
case .simulator(.iPadAir2): break // You're running on the iPad Air 2 simulator
|
|
|
178 |
default: break
|
|
|
179 |
}
|
|
|
180 |
```
|
|
|
181 |
|
|
|
182 |
### Make Sure the Device Is Contained in a Preconfigured Group
|
|
|
183 |
```swift
|
|
|
184 |
let groupOfAllowedDevices: [Device] = [.iPhone6, .iPhone6Plus, .iPhone6s, .iPhone6sPlus, .simulator(.iPhone6), .simulator(.iPhone6Plus),.simulator(.iPhone6s),.simulator(.iPhone6sPlus).simulator(.iPhone8),.simulator(.iPhone8Plus),.simulator(.iPhoneX),.simulator(.iPhoneXS),.simulator(.iPhoneXSMax),.simulator(.iPhoneXR)]
|
|
|
185 |
|
|
|
186 |
let device = Device.current
|
|
|
187 |
|
|
|
188 |
if device.isOneOf(groupOfAllowedDevices) {
|
|
|
189 |
// Do your action
|
|
|
190 |
}
|
|
|
191 |
```
|
|
|
192 |
|
|
|
193 |
### Get the Current Battery State
|
|
|
194 |
**Note:**
|
|
|
195 |
|
|
|
196 |
> To get the current battery state we need to set `UIDevice.current.isBatteryMonitoringEnabled` to `true`. To avoid any issues with your code, we read the current setting and reset it to what it was before when we're done.
|
|
|
197 |
|
|
|
198 |
```swift
|
|
|
199 |
if device.batteryState == .full || device.batteryState >= .charging(75) {
|
|
|
200 |
print("Your battery is happy! 😊")
|
|
|
201 |
}
|
|
|
202 |
```
|
|
|
203 |
|
|
|
204 |
### Get the Current Battery Level
|
|
|
205 |
```swift
|
|
|
206 |
if device.batteryLevel >= 50 {
|
|
|
207 |
install_iOS()
|
|
|
208 |
} else {
|
|
|
209 |
showError()
|
|
|
210 |
}
|
|
|
211 |
```
|
|
|
212 |
|
|
|
213 |
### Get Low Power mode status
|
|
|
214 |
```swift
|
|
|
215 |
if device.batteryState.lowPowerMode {
|
|
|
216 |
print("Low Power mode is enabled! 🔋")
|
|
|
217 |
} else {
|
|
|
218 |
print("Low Power mode is disabled! 😊")
|
|
|
219 |
}
|
|
|
220 |
```
|
|
|
221 |
|
|
|
222 |
### Check if a Guided Access session is currently active
|
|
|
223 |
```swift
|
|
|
224 |
if device.isGuidedAccessSessionActive {
|
|
|
225 |
print("Guided Access session is currently active")
|
|
|
226 |
} else {
|
|
|
227 |
print("No Guided Access session is currently active")
|
|
|
228 |
}
|
|
|
229 |
```
|
|
|
230 |
|
|
|
231 |
### Get Screen Brightness
|
|
|
232 |
```swift
|
|
|
233 |
if device.screenBrightness > 50 {
|
|
|
234 |
print("Take care of your eyes!")
|
|
|
235 |
}
|
|
|
236 |
```
|
|
|
237 |
|
|
|
238 |
### Get Available Disk Space
|
|
|
239 |
```swift
|
|
|
240 |
if Device.volumeAvailableCapacityForOpportunisticUsage ?? 0 > Int64(1_000_000) {
|
|
|
241 |
// download that nice-to-have huge file
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
if Device.volumeAvailableCapacityForImportantUsage ?? 0 > Int64(1_000) {
|
|
|
245 |
// download that file you really need
|
|
|
246 |
}
|
|
|
247 |
```
|
|
|
248 |
|
|
|
249 |
## Source of Information
|
|
|
250 |
All model identifiers are taken from the following website: https://www.theiphonewiki.com/wiki/Models or extracted from the simulator app bundled with Xcode.
|
|
|
251 |
|
|
|
252 |
## Contributing
|
|
|
253 |
If you have the need for a specific feature that you want implemented or if you experienced a bug, please open an issue.
|
|
|
254 |
If you extended the functionality of DeviceKit yourself and want others to use it too, please submit a pull request.
|
|
|
255 |
|
|
|
256 |
## Contributors
|
|
|
257 |
The complete list of people who contributed to this project is available [here](https://github.com/devicekit/DeviceKit/graphs/contributors). DeviceKit wouldn't be what it is without you! Thank you very much! 🙏
|