Detect current device with UIUserInterfaceIdiom in Swift


To detect current device with iOS/Swift we can use UserInterfaceIdiom. It is an enum in swift, which tells which device is being used.

The interface idiom provides multiple values in it’s enum which are.

case unspecified
@available(iOS 3.2, *)
case phone // iPhone and iPod touch style UI
@available(iOS 3.2, *)
case pad // iPad style UI
@available(iOS 9.0, *)
case tv // Apple TV style UI
@available(iOS 9.0, *)
case carPlay // CarPlay style UI

In swift interfaceIdiom can be used in the following way:

print(UIDevice.current.userInterfaceIdiom)
if UIDevice.current.userInterfaceIdiom == .phone {
   print("running on iPhone")
}

When we run the above code on an iPhone device following is the result produced.

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements