73

GitHub - RockerHX/Devices: Devices can detect the current  device base system i...

 6 years ago
source link: https://github.com/RockerHX/Devices
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Devices

Version Swift Xcode Platform Supported License

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

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

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK