Autoría | Ultima modificación | Ver Log |
<img src="/DeviceKit.svg" height="128">[](https://raw.githubusercontent.com/devicekit/DeviceKit/master/LICENSE)[](https://cocoapods.org/pods/DeviceKit)[](https://github.com/Carthage/Carthage)[](https://codecov.io/gh/devicekit/DeviceKit)[](https://cocoapods.org/pods/DeviceKit)[](https://codeclimate.com/github/devicekit/DeviceKit/maintainability)[](http://cocoadocs.org/docsets/DeviceKit)| Branch | Build Status | Versions ||:---------|:--------------:|:----------:|| **master** |[](https://travis-ci.org/devicekit/DeviceKit)| ≥ 2.0 || **Swift 4 - 4.2** |[](https://travis-ci.org/devicekit/DeviceKit)| ≥ 1.3 < 1.13 || **Swift 3** |[](https://travis-ci.org/devicekit/DeviceKit)| ≥ 1.0 < 1.3 || **Swift 2.3** |[](https://travis-ci.org/devicekit/DeviceKit)| < 1.0 |`DeviceKit` is a value-type replacement of [`UIDevice`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/).## Current version 4.6.0.See our detailed [changelog](CHANGELOG.md) for the latest features, improvements and bug fixes.## 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- [x] Apple Pencil support detection## Requirements- iOS 9.0+ (linking against iOS 9.3 required)- tvOS 9.0+ (linking against tvOS 9.2 required)- watchOS 2.0+## InstallationDeviceKit can be installed in various ways.### CocoaPods#### Swift 5```rubypod 'DeviceKit', '~> 4.0'```#### iOS 8.0 support```rubypod 'DeviceKit', '3.2'```#### Swift 4.0 - Swift 4.2```rubypod 'DeviceKit', '~> 1.3'```#### Swift 3```rubypod 'DeviceKit', '~> 1.2.3'```#### Swift 2.3 (Unsupported)```rubypod 'DeviceKit', :git => 'https://github.com/devicekit/DeviceKit.git', :branch => 'swift-2.3-unsupported'```### Swift Package Manager#### Swift 5```swiftdependencies: [.package(url: "https://github.com/devicekit/DeviceKit.git", from: "4.0.0"),/// ...]```#### iOS 8.0 support```swiftdependencies: [.package(url: "https://github.com/devicekit/DeviceKit.git", from: "3.2.0"),/// ...]```### Carthage#### Swift 5```ogdlgithub "devicekit/DeviceKit" ~> 4.0```#### iOS 8.0 support```ogdlgithub "devicekit/DeviceKit" ~> 3.2```#### Swift 4.0 - Swift 4.2```ogdlgithub "devicekit/DeviceKit" ~> 1.3```#### Swift 3```ogdlgithub "devicekit/DeviceKit" ~> 1.2.3```#### Swift 2.3 (Unsupported)```ogdlgithub "devicekit/DeviceKit" "swift-2.3-unsupported"```### ManuallyTo install it manually, drag the `DeviceKit` project into your app project in Xcode. Or add it as a git submodule by running:```bash$ git submodule add https://github.com/devicekit/DeviceKit.git```## UsageFirst make sure to import the framework:```swiftimport DeviceKit```Here are some usage examples. All devices are also available as simulators:```swift.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```swiftlet device = Device.currentprint(device) // prints, for example, "iPhone 6 Plus"if device == .iPhone6Plus {// Do something} else {// Do something else}```### Get the Device Family```swiftlet device = Device.currentif 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```swiftlet device = Device.currentif device.isSimulator {// Running on one of the simulators(iPod/iPhone/iPad)// Skip doing something irrelevant for Simulator}```### Get the Simulator Device```swiftlet device = Device.currentswitch device {case .simulator(.iPhone6s): break // You're running on the iPhone 6s simulatorcase .simulator(.iPadAir2): break // You're running on the iPad Air 2 simulatordefault: break}```### Make Sure the Device Is Contained in a Preconfigured Group```swiftlet 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.currentif 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` 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.```swiftif device.batteryState == .full || device.batteryState >= .charging(75) {print("Your battery is happy! 😊")}```### Get the Current Battery Level```swiftif device.batteryLevel >= 50 {install_iOS()} else {showError()}```### Get Low Power mode status```swiftif 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```swiftif device.isGuidedAccessSessionActive {print("Guided Access session is currently active")} else {print("No Guided Access session is currently active")}```### Get Screen Brightness```swiftif device.screenBrightness > 50 {print("Take care of your eyes!")}```### Get Available Disk Space```swiftif 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 InformationAll model identifiers are taken from the following website: https://www.theiphonewiki.com/wiki/Models or extracted from the simulator app bundled with Xcode.## ContributingIf 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.## ContributorsThe 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! 🙏