Detect Current Device With Ui_User_Interface_Idiom() in Swift

Detect current device with UI_USER_INTERFACE_IDIOM() in Swift

When working with Swift, you can use the enum UIUserInterfaceIdiom, defined as:

enum UIUserInterfaceIdiom : Int {
case unspecified

case phone // iPhone and iPod touch style UI
case pad // iPad style UI (also includes macOS Catalyst)
}

So you can use it as:

UIDevice.current.userInterfaceIdiom == .pad
UIDevice.current.userInterfaceIdiom == .phone
UIDevice.current.userInterfaceIdiom == .unspecified

Or with a Switch statement:

    switch UIDevice.current.userInterfaceIdiom {
case .phone:
// It's an iPhone
case .pad:
// It's an iPad (or macOS Catalyst)

@unknown default:
// Uh, oh! What could it be?
}

UI_USER_INTERFACE_IDIOM() is an Objective-C macro, which is defined as:

#define UI_USER_INTERFACE_IDIOM() \ ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \ [[UIDevice currentDevice] userInterfaceIdiom] : \ UIUserInterfaceIdiomPhone)

Also, note that even when working with Objective-C, the UI_USER_INTERFACE_IDIOM() macro is only required when targeting iOS 3.2 and below. When deploying to iOS 3.2 and up, you can use [UIDevice userInterfaceIdiom] directly.

How to detect current device using SwiftUI?

You can find the device type, like this:

if UIDevice.current.userInterfaceIdiom == .pad {
...
}

Checking if device is iPad

You can use

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

How to check if device is an iPad or iPhone not working

if UIDevice.current.userInterfaceIdiom == .pad {
print("iPad")
}else{
print("not iPad")
}

But you need to make your app a universal app.

iOS detect if user is on an iPad

There are quite a few ways to check if a device is an iPad. This is my favorite way to check whether the device is in fact an iPad:

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
return YES; /* Device is iPad */
}

The way I use it

#define IDIOM    UI_USER_INTERFACE_IDIOM()
#define IPAD UIUserInterfaceIdiomPad

if ( IDIOM == IPAD ) {
/* do something specifically for iPad. */
} else {
/* do something specifically for iPhone or iPod touch. */
}

Other Examples

if ( [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"] ) {
return YES; /* Device is iPad */
}

#define IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
if ( IPAD )
return YES;

For a Swift solution, see this answer: https://stackoverflow.com/a/27517536/2057171

Best way to programmatically detect iPad/iPhone hardware

I'm answering this now (and at this late date) because many of the existing answers are quite old, and the most Up Voted actually appears to be wrong according to Apples current docs (iOS 8.1, 2015)!

To prove my point, this is the comment from Apples header file (always look at the Apple source and headers):

/*The UI_USER_INTERFACE_IDIOM() macro is provided for use when
deploying to a version of the iOS less than 3.2. If the earliest
version of iPhone/iOS that you will be deploying for is 3.2 or
greater, you may use -[UIDevice userInterfaceIdiom] directly.*/

Therefore, the currently APPLE recommended way to detect iPhone vs. iPad, is as follows:

1) (DEPRECATED as of iOS 13) On versions of iOS PRIOR to 3.2, use the Apple provided macro:

// for iPhone use UIUserInterfaceIdiomPhone
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

2) On versions of iOS 3.2 or later, use the property on [UIDevice currentDevice]:

// for iPhone use UIUserInterfaceIdiomPhone
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)

UI_USER_INTERFACE_IDIOM() crashes app distributed on devices ONLY

I got it (partially!). Actually "release" implementation of UI_USER_INTERFACE_IDIOM() in swift project crashes the app.

Once I edit the scheme to "release" (Xcode > Product > Scheme > Edit Scheme > Run > Build Configuration change to "Release") and then run on simulator/device, the app crashes everywhere.. all devices/simulators/developer/distribution profiles.

However, still I have no clue why our app store app (objective c language based) does NOT crash.

My only guess is that it's a glitch in UI_USER_INTERFACE_IDIOM() API implementation with some language specific coding (swift vs objective c) by Apple.

Anyways, I would replace all UI_USER_INTERFACE_IDIOM() with UIDevice(). userInterfaceIdiom. I hope this helps someone!

How to detect iPod and iPhone device with Swift 3?

You can get it better by making an extension for UIDevice like:

public extension UIDevice {

var modelName: String {
var systemInfo = utsname()
uname(&systemInfo)
let machineMirror = Mirror(reflecting: systemInfo.machine)
let identifier = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else { return identifier }
return identifier + String(UnicodeScalar(UInt8(value)))
}

switch identifier {
case "iPod5,1": return "iPod Touch 5"
case "iPod7,1": return "iPod Touch 6"
case "iPhone3,1", "iPhone3,2", "iPhone3,3": return "iPhone 4"
case "iPhone4,1": return "iPhone 4s"
case "iPhone5,1", "iPhone5,2": return "iPhone 5"
case "iPhone5,3", "iPhone5,4": return "iPhone 5c"
case "iPhone6,1", "iPhone6,2": return "iPhone 5s"
case "iPhone7,2": return "iPhone 6"
case "iPhone7,1": return "iPhone 6 Plus"
case "iPhone8,1": return "iPhone 6s"
case "iPhone9,1", "iPhone9,3": return "iPhone 7"
case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"
case "i386", "x86_64": return "Simulator"
case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad 3"
case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad 4"
case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air"
case "iPad5,3", "iPad5,4": return "iPad Air 2"
case "iPad2,5", "iPad2,6", "iPad2,7": return "iPad Mini"
case "iPad4,4", "iPad4,5", "iPad4,6": return "iPad Mini 2"
case "iPad4,7", "iPad4,8", "iPad4,9": return "iPad Mini 3"
case "iPad5,1", "iPad5,2": return "iPad Mini 4"
case "iPad6,7", "iPad6,8": return "iPad Pro"
case "AppleTV5,3": return "Apple TV"
case "i386", "x86_64": return "Simulator"
default: return identifier
}
}
}

Usage: UIDevice.current.modelName
This will return the device model in string.

Try to use it like:

 if UIDevice.current.modelName == "Simulator" {
print("Simulator")
}
else {
print("Real Device")
}


Related Topics



Leave a reply



Submit