Device
is a value-type replacement of UIDevice
.
##Features
- Device identification
- Device family detection
- Device group detection
- Simulator detection
- Battery state
- Battery level
- Equatable
- iOS 8.0+ / tvOS 9.0+
- Xcode 7.3+
Version 0.3.1 requires Xcode 7.3 because it contains a fix for the UIUserInterfaceIdiom
wrapper enum that would not build otherwise (specifically you need to link against the iOS 9.3 SDK). If you still use Xcode 7.2.1 or lower (and do not link against the latest SDK) please use version 0.3.0.
DeviceKit can be installed in various ways.
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'DeviceKit', '~> 0.3.1'
github "dennisweissmann/DeviceKit" ~> 0.3.1
To install it manually drag the DeviceKit project into your app project in Xcode or add it as a git submodule. In your project folder enter:
$ git submodule add https://github.com/dennisweissmann/DeviceKit.git
Here are some usage examples. All devices are also available as simulators:
.iPhone6 => .Simulator(.iPhone6)
.iPhone6s => .Simualtor(.iPhone6s)
etc.
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 you action
}
if device.batteryState == .Full || device.batteryState >= .Charging(75) {
print("Your battery is happy! 😊")
}
if device.batteryLevel >= 50 {
install_iOS()
} else {
showError()
}
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.