Hardware Utilities

For iOS, tvOS, watchOS and macOS development with Swift and Xcode

Available exclusively as part of the Action Utilities framework and open sourced on GitHub under the MIT license.

Action UtilitiesOpen Source

Hardware Utilities

Hardware Utilities provide commonly used information about the device an app is running on such as the device type (for example iPhoneX), the OS version (for example iOS 11.1) and if the app can connect to the internet.

The following is included:

  • AppleHardwareType – Enum that contains a human-readable Apple Device Type (such as iPhoneX).
  • HardwareInformation – Provides information about the device an app is running on.

AppleHardwareType

Used to convert an Apple device model name (in the form iPhone10,3) to a human readable form (such as iPhoneX). This enum works with the HardwareInformationclass to get the type of device the app is running on.

Known Device Types

The following device types are handled:

  • iPhone
  • iPad
  • iPod Touch
  • Apple TV
  • AirPods
  • Apple Watch
  • HomePod
  • MacBook
  • MacBook Air
  • MacBook Pro
  • Mac Mini
  • Mac Pro
  • iMac

Example:

// Get the iPhoneX device type
let device = AppleHardwareType(fromModel: "iPhone10,3") 

HardwareInformation

Defines a set of convenience properties and functions when working on Apple devices, such as checking the device model name (iPhone10,3), getting the device type (iPhoneX), getting the OS version (iOS 11.1), the current device orientation and internet connection state.

The following properties are available:

isPhone

Returns true if the app is running on an iPhone, else returns false. For example:

if HardwareInformation.isPhone {
    // App is running on an iPhone
    ...
}

isPad

Returns true if the app is running on an iPad, else returns false. For example:

if HardwareInformation.isPad {
    // App is running on an iPad
    ...
}

isTV

Returns true if the app is running on an Apple TV, else returns false. For example:

if HardwareInformation.isTV {
    // App is running on an Apple TV
    ...
}

isCar

Returns true if the app is running on a CarPlay connected device, else returns false. For example:

if HardwareInformation.isCar {
    // App is running on CarPlay
    ...
}

isWatch

Returns true if the app is running on an Apple Watch, else returns false. For example:

if HardwareInformation.isWatch {
    // App is running on an Apple Watch
    ...
}

isMac

Returns true if the app is running on a Mac laptop or desktop, else returns false. For example:

if HardwareInformation.isMac {
    // App is running on a Mac
    ...
}

modelName

Returns the model name of the device the app is running on. For example, iPhone10,3 or iPhone10,6 for the iPhone X. For example:

// Get the device model name (for example iPhone10,3)
let model = HardwareInformation.modelName 

deviceType

Returns the human-readable type of the device that the app is running on (for example iPhoneX) or unknown if the type cannot be discovered. For example:

if HardwareInformation.deviceType == .iPhoneX {
    // The app is running on an iPhone X
    ...
}

osVersion

Returns the version number (for example iOS 11.1) of the OS installed on the device that the app is running on. For example:

let version = HardwareInformation.osVersion

deviceOrientation

Returns the current orientation of the device that the app is running on. If the orientation is unknown, a guess will be made based on the main screen’s bounds: if the width is less than the height, portrait is returned, else landscapeLeft is returned. For example:

if HardwareInformation.deviceOrientation == .portrait {
    // The device is in the portrait position
    ...
}

isRetinaDisplay

Returns true if the device the app is running on has a retina display, else returns false. For example:

if HardwareInformation.isRetinaDisplay {
    // The device has a Retina display
    ...
}

isHDDisplay

Returns true if the device the app is running on has a HD display, else returns false. For example:

if HardwareInformation.isHDDisplay {
    // The device has a HD display
    ...
}

screenBounds

Returns the bounds of the main screen of the device that the app is running on. As of iOS 8.0, these bounds will always be relative to the device’s orientation. For example:

let bounds = HardwareInformation.screenBounds

isConnectedToNetwork

Returns true if the device the app is running on is connected to the internet, else returns false. For example:

if HardwareInformation.isConnectedToNetwork {
    // The device has an internet connection
    ...
}

isWWANNetworkConnection

Returns true if the device the app is running on is connected to the internet via a cellular connection, such as EDGE or GPRS, else returns false. For example:

if HardwareInformation.isWWANNetworkConnection {
    // The device has an internet via cellular
    ...
}