Latest | 2.0.0 |
---|---|
Homepage | https://github.com/devicekit/DeviceKit |
License | MIT |
Platforms | ios 8.0, tvos 9.0, watchos 2.0, requires ARC |
Branch | Build Status | Versions |
---|---|---|
master | ≥ 2.0 | |
Swift 4 – 4.2 | ≥ 1.3 < 1.13 | |
Swift 3 | ≥ 1.0 < 1.3 | |
Swift 2.3 | < 1.0 |
DeviceKit
is a value-type replacement of UIDevice
.
Changelog
Here you find an overview about all the latest features, bugfixes and breaking changes shipped with version 2.0 which was released on 10th April 2019.
Breaking changes version 2.0
- The original
Device()
constructor has been made private in favour of usingDevice.current
to matchUIDevice.current
. - The enum values for the iPhone Xs, iPhone Xs Max and iPhone Xʀ have been renamed to be
.iPhoneXS
,.iPhoneXSMax
and.iPhoneXR
to match proper formatting. .description
for the iPhone Xs, iPhone Xs Max and iPhone Xʀ have been changed to contain small caps formatting for the s and the ʀ part..description
for the iPad 5 and iPad 6 have been changed to the proper names; iPad (5th generation) and iPad (6th generation)..name
,.systemName
,.systemVersion
,.model
,.localizedModel
,.batteryState
and.batteryLevel
will now all return nil when you try to get its value when the device you are getting it from isn’t the current one. (eg.Device.iPad6.name
while running on iPad 5)
New features
- Updated to Swift 5!
- New
.allDevicesWithRoundedDisplayCorners
and.hasRoundedDisplayCorners
values to check if a device has rounded display corners. (eg. iPhone Xs and iPad Pro (3rd generation)) - new
.allDevicesWithSensorHousing
and.hasSensorHousing
values to check if a device has a screen cutout for the sensor housing. (eg. iPhone Xs)
Bugfixes
.isPad
and.isPhone
are now giving correct outputs again.
Features
- [x] Equatable
- [x] Device identification
- [x] Device family detection
- [x] Device group detection
- [x] Simulator detection
- [x] Battery state
- [x] Battery level
- [x] Various device metrics (e.g. screen size, screen ratio, PPI)
- [x] Low Power Mode detection
- [x] Guided Access Session detection
- [x] Screen brightness
- [x] Display Zoom detection
- [x] Detect available sensors (Touch ID, Face ID)
- [x] Detect available disk space
Requirements
- iOS 8.0+ (linking against iOS 9.3 required)
- tvOS 9.0+ (linking against tvOS 9.2 required)
- watchOS 2.0+
Installation
DeviceKit can be installed in various ways.
CocoaPods
Swift 5
pod 'DeviceKit', '~> 2.0'
Swift 4.0 – Swift 4.2
pod 'DeviceKit', '~> 1.3'
Swift 3
pod 'DeviceKit', '~> 1.2.3'
Swift 2.3 (Unsupported)
pod 'DeviceKit', :git => 'https://github.com/devicekit/DeviceKit.git', :branch => 'swift-2.3-unsupported'
Carthage
Swift 5
github "devicekit/DeviceKit" ~> 2.0
Swift 4.0 – Swift 4.2
github "devicekit/DeviceKit" ~> 1.3
Swift 3
github "devicekit/DeviceKit" ~> 1.2.3
Swift 2.3 (Unsupported)
github "devicekit/DeviceKit" "swift-2.3-unsupported"
Manually
To install it manually, drag the DeviceKit
project into your app project in Xcode. Or add it as a git submodule by running:
$ git submodule add https://github.com/devicekit/DeviceKit.git
Usage
First make sure to import the framework:
import DeviceKit
Here are some usage examples. All devices are also available as simulators:
.iPhone6 => .simulator(.iPhone6)
.iPhone6s => .simulator(.iPhone6s)
You can try these examples in Playground.
Note:
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.
Get the Device You’re Running On
let device = Device.current
print(device) // prints, for example, "iPhone 6 Plus"
if device == .iPhone6Plus {
// Do something
} else {
// Do something else
}
Get the Device Family
let device = Device.current
if device.isPod {
// iPods (real or simulator)
} else if device.isPhone {
// iPhone (real or simulator)
} else if device.isPad {
// iPad (real or simulator)
}
Check If Running on Simulator
let device = Device.current
if device.isSimulator {
// Running on one of the simulators(iPod/iPhone/iPad)
// Skip doing something irrelevant for Simulator
}
Get the Simulator Device
let device = Device.current
switch device {
case .simulator(.iPhone6s): break // You're running on the iPhone 6s simulator
case .simulator(.iPadAir2): break // You're running on the iPad Air 2 simulator
default: break
}
Make Sure the Device Is Contained in a Preconfigured Group
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)]
let device = Device.current
if device.isOneOf(groupOfAllowedDevices) {
// Do your action
}
Get the Current Battery State
Note:
To get the current battery state we need to set
UIDevice.current.isBatteryMonitoringEnabled
totrue
. To avoid any issues with your code, we read the current setting and reset it to what it was before when we’re done.
if device.batteryState == .full || device.batteryState >= .charging(75) {
print("Your battery is happy! 😊")
}
Get the Current Battery Level
if device.batteryLevel >= 50 {
install_iOS()
} else {
showError()
}
Get Low Power mode status
if device.batteryState.lowPowerMode {
print("Low Power mode is enabled! 🔋")
} else {
print("Low Power mode is disabled! 😊")
}
Check if a Guided Access session is currently active
if device.isGuidedAccessSessionActive {
print("Guided Access session is currently active")
} else {
print("No Guided Access session is currently active")
}
Get Screen Brightness
if device.screenBrightness > 50 {
print("Take care of your eyes!")
}
Get Available Disk Space
if Device.volumeAvailableCapacityForOpportunisticUsage ?? 0 > Int64(1_000_000) {
// download that nice-to-have huge file
}
if Device.volumeAvailableCapacityForImportantUsage ?? 0 > Int64(1_000) {
// download that file you really need
}
Source of Information
All model identifiers are taken from the following website: https://www.theiphonewiki.com/wiki/Models or extracted from the simulator app bundled with Xcode.
Contributing
If you have the need for a specific feature that you want implemented or if you experienced a bug, please open an issue.
If you extended the functionality of DeviceKit yourself and want others to use it too, please submit a pull request.
Contributors
The complete list of people who contributed to this project is available here. DeviceKit wouldn’t be what it is without you! Thank you very much! 🙏
Latest podspec
{ "name": "DeviceKit", "version": "2.0.0", "summary": "DeviceKit is a u00b5-framework that provides a value-type replacement of UIDevice.", "description": "`DeviceKit` is a value-type wrapper and extension of [`UIDevice`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/). It detects both devices and different simulators.", "homepage": "https://github.com/devicekit/DeviceKit", "license": "MIT", "authors": "DeviceKit", "social_media_url": "https://twitter.com/dennis_weissman", "requires_arc": true, "platforms": { "ios": "8.0", "tvos": "9.0", "watchos": "2.0" }, "source": { "git": "https://github.com/devicekit/DeviceKit.git", "tag": "2.0.0" }, "source_files": "Source/Device.generated.swift" }
Sun, 28 Apr 2019 10:17:05 +0000