Convert Figma logo to code with AI

devicekit logoDeviceKit

DeviceKit is a value-type replacement of UIDevice.

4,433
432
4,433
57

Top Related Projects

Detects the hardware, software and display of the current iOS or Mac OS X device at runtime.

DeviceKit is a value-type replacement of UIDevice.

Quick Overview

DeviceKit is a Swift library that provides a simple way to identify and query iOS devices. It offers a more friendly interface than Apple's UIDevice class, allowing developers to easily access device information and capabilities.

Pros

  • Easy to use and integrate into Swift projects
  • Provides more detailed device information than UIDevice
  • Regularly updated to support new device models
  • Lightweight and has no dependencies

Cons

  • Limited to iOS devices only
  • May require frequent updates to stay current with new device releases
  • Some advanced features might not be available for older devices
  • Relies on unofficial device identifiers, which could potentially change

Code Examples

Identifying the current device:

import DeviceKit

let device = Device.current

if device.isPad {
    print("This is an iPad")
} else if device.isPhone {
    print("This is an iPhone")
}

Checking device capabilities:

if device.hasBiometricSensor {
    if device.hasFaceID {
        print("This device has Face ID")
    } else if device.hasTouchID {
        print("This device has Touch ID")
    }
}

Comparing devices:

if device >= .iPhone8 {
    print("This device is an iPhone 8 or newer")
}

if device.isOneOf([.iPhoneX, .iPhoneXS, .iPhoneXSMax, .iPhoneXR]) {
    print("This is an iPhone X series device")
}

Getting Started

  1. Add DeviceKit to your project using Swift Package Manager:

    dependencies: [
        .package(url: "https://github.com/devicekit/DeviceKit.git", from: "4.0.0")
    ]
    
  2. Import DeviceKit in your Swift file:

    import DeviceKit
    
  3. Start using the Device.current property to access device information:

    let device = Device.current
    print("Device name: \(device.name)")
    print("Device model: \(device.model)")
    

Competitor Comparisons

Detects the hardware, software and display of the current iOS or Mac OS X device at runtime.

Pros of GBDeviceInfo

  • Supports a wider range of platforms (iOS, macOS, tvOS, watchOS)
  • Provides more detailed hardware information (e.g., CPU frequency, memory size)
  • Includes additional features like jailbreak detection

Cons of GBDeviceInfo

  • Less frequently updated compared to DeviceKit
  • More complex API, potentially requiring more setup and configuration
  • Larger codebase, which may impact app size and compilation time

Code Comparison

GBDeviceInfo:

let deviceInfo = GBDeviceInfo()
let deviceModel = deviceInfo.model
let cpuFrequency = deviceInfo.cpuFrequency

DeviceKit:

let device = Device.current
let deviceModel = device.model
let batteryState = device.batteryState

Summary

GBDeviceInfo offers more comprehensive device information across multiple platforms, making it suitable for apps requiring detailed hardware specs. However, DeviceKit provides a simpler API and more frequent updates, which may be preferable for projects focused primarily on iOS devices and basic device information. The choice between the two depends on the specific requirements of your project and the platforms you need to support.

DeviceKit is a value-type replacement of UIDevice.

Pros of DeviceKit

  • More actively maintained with recent updates
  • Larger community and contributor base
  • Comprehensive documentation and examples

Cons of DeviceKit

  • Slightly larger codebase, potentially impacting app size
  • May include features not needed for simpler projects

Code Comparison

DeviceKit:

let device = Device.current
if device.isPad {
    print("Running on iPad")
} else if device.isPhone {
    print("Running on iPhone")
}

DeviceKit:

let device = Device.current
if device.isPad {
    print("Running on iPad")
} else if device.isPhone {
    print("Running on iPhone")
}

The code comparison shows that both repositories use similar syntax and structure for device detection. This is because DeviceKit is the same repository as devicekit/DeviceKit. The comparison requested appears to be between the same project, so there are no significant differences to highlight in terms of functionality or usage.

Both versions of DeviceKit provide a simple and intuitive way to detect device types and characteristics in Swift. They offer a wide range of properties and methods to identify specific device models, screen sizes, and capabilities.

For developers looking to implement device detection in their iOS apps, DeviceKit remains a popular and reliable choice, regardless of which repository URL is used to access it.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

GitHub license CocoaPods Compatible Carthage Compatible codecov CocoaPods Maintainability Platform

BranchVersions
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.

Current version 5.5.0

See our detailed changelog for the latest features, improvements and bug fixes.

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
  • Apple Pencil support detection

Requirements

  • iOS 11.0+
  • tvOS 11.0+
  • watchOS 4.0+

Installation

DeviceKit can be installed in various ways.

CocoaPods

Swift 5

pod 'DeviceKit', '~> 5.2'

iOS 8.0 support

pod 'DeviceKit', '3.2'

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'

Swift Package Manager

Swift 5

dependencies: [
    .package(url: "https://github.com/devicekit/DeviceKit.git", from: "4.0.0"),
    /// ...
]

iOS 8.0 support

dependencies: [
    .package(url: "https://github.com/devicekit/DeviceKit.git", from: "3.2.0"),
    /// ...
]

Carthage

Swift 5

github "devicekit/DeviceKit" ~> 4.0

iOS 8.0 support

github "devicekit/DeviceKit" ~> 3.2

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