Device
Light weight tool for detecting the current device and screen size written in swift.
Top Related Projects
DeviceKit is a value-type replacement of UIDevice.
Quick Overview
Device is a lightweight Swift library for detecting the current iOS device model and screen size. It provides an easy way to identify the specific device a user is running your app on, which can be useful for device-specific optimizations or feature implementations.
Pros
- Simple and easy to use API
- Regularly updated to support new device models
- Lightweight with no external dependencies
- Supports both UIKit and SwiftUI
Cons
- Limited to iOS devices only
- Relies on internal device identifiers, which may change in future iOS versions
- May require frequent updates to stay current with new device releases
- Does not provide detailed hardware information beyond model and screen size
Code Examples
Detecting the current device model:
import Device
let device = Device.current
switch device {
case .iPhone8: print("iPhone 8")
case .iPhoneXS: print("iPhone XS")
case .iPhone12Pro: print("iPhone 12 Pro")
default: print("Other iPhone model")
}
Checking the screen size:
import Device
let size = Device.size()
switch size {
case .screen4_7Inch: print("4.7-inch screen")
case .screen5_8Inch: print("5.8-inch screen")
case .screen6_1Inch: print("6.1-inch screen")
default: print("Other screen size")
}
Determining if the device has a notch:
import Device
if Device.hasNotch {
print("This device has a notch")
} else {
print("This device does not have a notch")
}
Getting Started
To use Device in your project, add the following line to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/Ekhoo/Device.git", from: "3.3.0")
]
Then, import the module in your Swift file:
import Device
Now you can use the Device
enum and its properties to detect the current device and its characteristics.
Competitor Comparisons
DeviceKit is a value-type replacement of UIDevice.
Pros of DeviceKit
- More actively maintained with recent updates
- Supports Swift Package Manager for easier integration
- More comprehensive device detection, including newer iPhone and iPad models
Cons of DeviceKit
- Larger codebase, potentially increasing app size
- May have a steeper learning curve due to more features
Code Comparison
DeviceKit:
let device = Device.current
if device.isPad {
print("Running on iPad")
} else if device.isPhone {
print("Running on iPhone")
}
Device:
let device = Device()
if device.isPhone {
print("Running on iPhone")
} else if device.isPad {
print("Running on iPad")
}
Both libraries provide similar functionality for device detection, but DeviceKit offers a more extensive range of properties and methods for detailed device information. DeviceKit uses a static current
property, while Device requires instantiation.
DeviceKit is generally more feature-rich and up-to-date, making it a better choice for projects requiring detailed device information and support for newer models. However, Device may be simpler to use for basic device detection tasks.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Device detect the current  device model and screen size.
Installation
CocoaPods
Device is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Device", '~> 3.6.0'
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Device into your Xcode project using Carthage, specify it in your Cartfile
:
github "Ekhoo/Device" ~> 3.6.0
Run carthage update
to build the framework and drag the built Device.framework
into your Xcode project.
Swift Package Manager
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding Device as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/Ekhoo/Device.git", from: "3.6.0")
]
Usage
iOS
Device version
func myFunc() {
/*** Display the device version ***/
switch Device.version() {
/*** iPhone ***/
case .iPhone4: print("It's an iPhone 4")
case .iPhone4S: print("It's an iPhone 4S")
case .iPhone5: print("It's an iPhone 5")
case .iPhone5C: print("It's an iPhone 5C")
case .iPhone5S: print("It's an iPhone 5S")
case .iPhone6: print("It's an iPhone 6")
case .iPhone6S: print("It's an iPhone 6S")
case .iPhone6Plus: print("It's an iPhone 6 Plus")
case .iPhone6SPlus: print("It's an iPhone 6 S Plus")
case .iPhoneSE: print("It's an iPhone SE")
case .iPhone7: print("It's an iPhone 7")
case .iPhone7Plus: print("It's an iPhone 7 Plus")
case .iPhone8: print("It's an iPhone 8")
case .iPhone8Plus: print("It's an iPhone 8 Plus")
case .iPhoneX: print("It's an iPhone X")
case .iPhoneXS: print("It's an iPhone Xs")
case .iPhoneXS_Max: print("It's an iPhone Xs Max")
case .iPhoneXR: print("It's an iPhone Xr")
/*** iPad ***/
case .iPad1: print("It's an iPad 1")
case .iPad2: print("It's an iPad 2")
case .iPad3: print("It's an iPad 3")
case .iPad4: print("It's an iPad 4")
case .iPad5: print("It's an iPad 5")
case .iPad6: print("It's an iPad 6")
case .iPadAir: print("It's an iPad Air")
case .iPadAir2: print("It's an iPad Air 2")
case .iPadMini: print("It's an iPad Mini")
case .iPadMini2: print("It's an iPad Mini 2")
case .iPadMini3: print("It's an iPad Mini 3")
case .iPadMini4: print("It's an iPad Mini 4")
case .iPadPro9_7Inch: print("It's an iPad Pro 9.7 Inch")
case .iPadPro10_5Inch: print("It's an iPad Pro 10.5 Inch")
case .iPadPro12_9Inch: print("It's an iPad Pro 12.9 Inch")
/*** iPod ***/
case .iPodTouch1Gen: print("It's a iPod touch generation 1")
case .iPodTouch2Gen: print("It's a iPod touch generation 2")
case .iPodTouch3Gen: print("It's a iPod touch generation 3")
case .iPodTouch4Gen: print("It's a iPod touch generation 4")
case .iPodTouch5Gen: print("It's a iPod touch generation 5")
case .iPodTouch6Gen: print("It's a iPod touch generation 6")
/*** Simulator ***/
case .Simulator: print("It's a Simulator")
/*** Unknown ***/
default: print("It's an unknown device")
}
}
Device screen size
func myFunc() {
/*** Display the device screen size ***/
switch Device.size() {
case .screen3_5Inch: print("It's a 3.5 inch screen")
case .screen4Inch: print("It's a 4 inch screen")
case .screen4_7Inch: print("It's a 4.7 inch screen")
case .screen5_5Inch: print("It's a 5.5 inch screen")
case .screen5_8Inch: print("It's a 5.8 inch screen")
case .screen6_1Inch: print("It's a 6.1 inch screen")
case .screen6_5Inch: print("It's a 6.8 inch screen")
case .screen7_9Inch: print("It's a 7.9 inch screen")
case .screen9_7Inch: print("It's a 9.7 inch screen")
case .screen10_5Inch: print("It's a 10.5 inch screen")
case .screen12_9Inch: print("It's a 12.9 inch screen")
default: print("Unknown size")
}
}
Device type
func myFunc() {
/*** Display the device type ***/
switch Device.type() {
case .iPod: print("It's an iPod")
case .iPhone: print("It's an iPhone")
case .iPad: print("It's an iPad")
case .Simulator: print("It's a Simulated device")
default: print("Unknown device type")
}
}
or
func myFunc() {
/*** Display the device type ***/
if (Device.isPad()){
print("It's an iPad")
}
else if (Device.isPhone()){
print("It's an iPhone")
}
else if (Device.isPod()){
print("It's an iPod")
}
else if (Device.isSimulator()){
print("It's a Simulated device")
}
}
Mac
Mac version
func myFunc() {
/*** Display the mac version ***/
switch Device.type() {
case .iMac: print("It's an iMac")
case .macBook: print("It's a MacBook")
case .macBookAir: print("It's a MacBook Air")
case .macBookPro: print("It's a MacBook Pro")
default: print("Unknown device type")
}
}
Mac screen size
func myFunc() {
/*** Display the mac screen size ***/
switch Device.size() {
case .screen11Inch: print("It's a 11 inch screen")
case .screen12Inch: print("It's a 12 inch screen")
case .screen13Inch: print("It's a 13 inch screen")
case .screen15Inch: print("It's a 15 inch screen")
case .screen17Inch: print("It's a 17 inch screen")
case .screen21_5Inch: print("It's a 21.5 inch screen")
case .screen27Inch: print("It's a 27 inch screen")
default: print("Unknown size")
}
}
Helpers
func myFunc() {
/*** Helpers ***/
if Device.size() == Size.screen4Inch {
print("It's a 4 inch screen")
}
if Device.size() > Size.screen4_7Inch {
print("Your device screen is larger than 4.7 inch")
}
if Device.size() < Size.screen4_7Inch {
print("Your device screen is smaller than 4.7 inch")
}
if Device.size() == Size.screen27Inch {
print("It's a 27 inch screen")
}
if Device.size() > Size.screen15Inch {
print("Your mac screen is larger than 15 inch")
}
if Device.size() < Size.screen15Inch {
print("Your mac screen is smaller than 15 inch")
}
if Device.isRetina() {
print("It's a retina display")
}
}
Video Tutorial by Alex Nagy (@rebeloper):
Privacy
Device does not collect any data. We provide this notice to help you fill out App Privacy Details. Since Device is not using any of privacy required reason API as listed by Apple here xcprivacy manifest is not needed for this SDK.
Author
Lucas Ortis:
Maintenance
Pran Kishore:
License
Device is available under the MIT license. See the LICENSE file for more info.
Top Related Projects
DeviceKit is a value-type replacement of UIDevice.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot