Detect current device with UIUserInterfaceIdiom in Swift

SwiftProgrammingMobile DevelopmentiOSiPhone/iPad

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.

raja
Published on 21-Jan-2019 16:39:12
Advertisements