Denise Nepraunig 6c8c16e437 Merge branch 'master' into swift-4.2
* master: (43 commits)
  version 1.13.0
  Fixed spelling mistake.
  Compile to generate the generated.swift file.
  Add device model identifiers.
  Fixed new iPad Air being seen as an iPad Pro.
  Add new iPad Air (3th generation) and iPad Mini (5th generation).
  Compile generated.swift
  Also update the gyb file
  fix volumeAvailableCapacityForImportantUsage error
  Fixed logo url in README.md
  add CocoaPodsVerification project
  update version to 1.12.0
  Update DeviceKit.podspec
  Update README after moving repo to DeviceKit org
  Rename allDevicesWithASensorHousing to allDevicesWithSensorHousing.
  Move variables together.
  Add boolean and array functions to check if the device has a sensor housing.
  Update README.md
  bump version number to 1.11.0
  [CI] add more simulators
  ...
2019-04-01 19:02:24 +02:00
2017-06-11 16:19:16 +02:00
2019-03-29 22:07:17 +01:00
2017-07-15 17:04:09 +02:00
2017-09-17 13:00:25 +02:00
2018-10-21 13:55:34 +02:00
2018-12-12 11:48:15 +01:00
2017-05-31 11:49:10 +02:00
2019-03-29 22:07:17 +01:00
2017-02-05 13:27:39 +01:00
2017-05-31 11:49:10 +02:00
2018-10-11 21:45:21 +02:00
2015-11-18 23:12:18 +01:00
2018-10-11 21:29:19 +02:00
2019-03-26 19:37:38 +01:00

GitHub license CocoaPods Compatible Carthage Compatible codecov CocoaPods Maintainability Platform

Branch Build Status Versions
master Build Status -
Swift 4 - 4.2 Build Status ≥ 1.3
Swift 3 Build Status ≥ 1.0 < 1.3
Swift 2.3 Build Status < 1.0

DeviceKit is a value-type replacement of UIDevice.

Features

  • Equatable
  • Device identification
  • Device family detection
  • Device group detection
  • Simulator detection
  • Battery state
  • Battery level
  • Various device metrics (e.g. screen size, screen ratio, PPI)
  • Low Power Mode detection
  • Guided Access Session detection
  • Screen brightness
  • Display Zoom detection
  • Detect available sensors (Touch ID, Face ID)
  • 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 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 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()

print(device)     // prints, for example, "iPhone 6 Plus"

if device == .iPhone6Plus {
  // Do something
} else {
  // Do something else
}

Get the Device Family

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)
}

Check If Running on Simulator

let device = Device()
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()
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()
 
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 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.

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! 🙏

S
Description
Languages
Swift 80.6%
Python 17.8%
Ruby 1.6%