Branch | Build Status | Versions |
---|---|---|
master | - | |
Swift 4 - 4.2 | ≥ 1.3 | |
Swift 3 | ≥ 1.0 < 1.3 | |
Swift 2.3 | < 1.0 |
DeviceKit
is a value-type replacement of UIDevice
.
DeviceKit can be installed in various ways.
pod 'DeviceKit', '~> 1.3'
pod 'DeviceKit', '~> 1.2.3'
pod 'DeviceKit', :git => 'https://github.com/dennisweissmann/DeviceKit.git', :branch => 'swift-2.3-unsupported'
github "dennisweissmann/DeviceKit" ~> 1.3
github "dennisweissmann/DeviceKit" ~> 1.2.3
github "dennisweissmann/DeviceKit" "swift-2.3-unsupported"
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/dennisweissmann/DeviceKit.git
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.
let device = Device() print(device) // prints, for example, "iPhone 6 Plus" if device == .iPhone6Plus { // Do something } else { // Do something else }
let device = Device() if device.isPod { // iPods (real or simulator) } else if device.isPhone { // iPhone (real or simulator) } else if device.isPad { // iPad (real or simulator) }
let device = Device() if device.isSimulator { // Running on one of the simulators(iPod/iPhone/iPad) // Skip doing something irrelevant for Simulator }
let device = Device() 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 }
let groupOfAllowedDevices: [Device] = [.iPhone6, .iPhone6Plus, .iPhone6s, .iPhone6sPlus, .simulator(.iPhone6), .simulator(.iPhone6Plus), .simulator(.iPhone6s), .simulator(.iPhone6sPlus)] let device = Device() if device.isOneOf(groupOfAllowedDevices) { // Do your action }
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! 😊") }
if device.batteryLevel >= 50 { install_iOS() } else { showError() }
if device.batteryState.lowPowerMode { print("Low Power mode is enabled! 🔋") } else { print("Low Power mode is disabled! 😊") }
if device.isGuidedAccessSessionActive { print("Guided Access session is currently active") } else { print("No Guided Access session is currently active") }
if device.screenBrightness > 50 { print("Take care of your eyes!") }
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 }
All model identifiers are taken from the following website: https://www.theiphonewiki.com/wiki/Models or extracted from the simulator app bundled with Xcode.
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.
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! 🙏