Skip to content

apialerts/idevice

Repository files navigation

iDevice

iDevice is a simple no dependency Kotlin library for the JVM that translates Apple device codes into their corresponding device names.

It is used in the API Alerts platform to determine the model name of iOS devices to show in the users' device list.

Installation

Add the dependency to your Gradle toml file:

idevice = { module = "com.apialerts:idevice", version = "<version>" }

or Groovy build.gradle file:

implementation 'com.apialerts:idevice:<version>'

Ensure mavenCentral() is added to your repository list

Usage

iOS App

Create an extension for UIDevice in your Swift iOS app to retrieve the device code:

// Swift
extension UIDevice {
    func modelName() -> String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let mirror = Mirror(reflecting: systemInfo.machine)

        let identifier = mirror.children.reduce("") { identifier, element in
          guard let value = element.value as? Int8, value != 0 else { return identifier }
          return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return identifier
    }
}

Use the extension to get the device model code:

// Swift
let model = UIDevice.current.modelName()

Backend (Kotlin/Java API)

Pass the device model code to your backend running a JVM API to decode it into a real device name:

// Kotlin
import com.apialerts.idevice.appleDeviceName

val model = appleDeviceName("<device code>")

If the device code is in the supported devices lookup table, it returns the device name.

Updating the Library

The repository needs to be updated when Apple releases new devices. Create a pull request with the new device code and model name to the lookup table in AppleDevices.kt.

Credits

This library is inspired by and credits DeviceKit.

About

iDevice is a simple no dependency Kotlin library for the JVM that translates Apple device codes into their corresponding device names.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages