/Devices

Devices can detect the current  device base system information. Support iOS, OSX, tvOS, watchOS.

Primary LanguageSwiftMIT LicenseMIT

Devices

Version Swift4.0 Swift5.0 Swift5.1 Swift5.2 Xcode Platform Supported License

Branch Build Status Versions
master Build Status -
Swift 5.2 Build Status ≥ 1.1.0
Swift 5.0 Build Status ≥ 1.0.0
Swift 4.2 Build Status ≥ 0.8.0
Below Swift 4.2 Build Status < 0.7.2

Features

  • Device identification
  • Device family detection
  • Device model detection
  • Device size detection
  • Device group detection
  • Simulator detection
  • Brightness detection
  • Battery detection
  • Equatable

Installation

CocoaPods

Devices is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Devices"

Usage

First make sure to import the framework:

import Devices

iOS

Device family

Simple code:

func iOSDeviceFamily() {
    let family = Device.family()
    switch family {
    case .iPhone:
        print("Device belong to \(family) family")
    case .unknown:
        print("unknown Device.")
    }
}
if Device.isFringe {
    code...
}

Demo code: Family Demo Code

Device model

Simple code:

func iOSDeviceModel() {
    let model = Device.model()
    print(model.identifier)
    switch model {
    /*** iPod ***/
    case .iPodTouch6Gen:
        print("Device is a \(model)")
    /*** iPhone ***/
    case .iPhoneX:
        print("Device is a \(model)")
    /*** iPad ***/
    case .iPadPro10_5Inch:
        print("Device is a \(model)")
    /*** HomePod ***/
    case .HomePod:
        print("Device is a \(model)")
    case .simulator(.iPhoneX):
        print("Device is a \(model)")
    /*** unknown ***/
    case .unknown:
        print("unknown Device.")
    }
}

Demo code: Model Demo Code

Device screen size

Simple code:

func iOSDeviceSize() {
    let size = Device.size()
    switch size {
    case .screen12_9Inch:
        print("Device size: \(size.description)")
    case .unknown:
        print("Device size unknown.")
    }
}

Demo code: Size Demo Code

Helpers

Model

Simple code:

func iOSModelHelper() {
    let model = Device.model()
    let allPhones = Device.Model.allPhones
    if allPhones.contains(model) {
        print("Current device belong to iPhone ")
    }
    let allSimulatorPhones = Device.Model.allSimulatorPhones
    if allSimulatorPhones.contains(model) {
        print("Current device belong to iPhone Simulator")
    }
}

Demo code: Model Helper Demo Code

Size

You can use operator like >, <, >=, <=, ==, !=

Simple code:

func iOSSizeHelper() {
    let size = Device.size()
    if size > .screen4_7Inch {
        print("Your device screen is larger than 4.7 inch")
    }

    if Device.isRetina() {
        print("It's a retina display")
    }
}

Demo code: Size Helper Demo Code

Brightness

Apple not provide api to get brightness on tvOS and watchOS, and if you want change watchOS's device brightness will be rejected. So, brightness api can only support iOS and OSX platform. iOS Simple code:

func iOSBrightness() {
    print("Device.brightness: \(Device.brightness)")
    Device.brightness = 0.8
}

OSX Simple code:

func OSXBrightness() {
    print("Device.Brightness.level: \(Device.Brightness.level)")
    Device.Brightness.level = .level_5
    Device.Brightness.level = .custom(0.5)
}

Battery

iOS Simple code:

func iOSBattery() {
    print(Device.Battery.state)
    let batteryState = Device.Battery.state

    switch batteryState {
    case .full:
        print("Your battery is happy! 😊")
    case .charging(let level):
        print("Your battery level: \(level)")
    case .unplugged(let level):
        print("Your battery level: \(level)")
    }

    if Device.Battery.lowPowerMode {
        print("Low Power mode is enabled! 🔋")
    } else {
        print("Low Power mode is disabled! 😊")
    }

    guard batteryState < .charging(80) else {
        print("Your battery is happy! 😊")
        return
    }
}

Project

iOS Simple code:

func iOSApplication() {
    print("Device is Fringe:\(Device.isFringe)")
    print("Build: \(Device.Application.shared.build)")
    print("Version: \(Device.Application.shared.version)")
    print("Name: \(Device.Application.shared.name)")
    print("wholeVersion: \(Device.Application.shared.wholeVersion)")
    print("firstLanuch: \(Device.Application.shared.firstLanuch)")
}

OSX

OSX Demo Code

tvOS

tvOS Demo Code

watchOS

watchOS Demo Code

License

Device is available under the MIT license. See the LICENSE file for more info.