We will explore ways to identify the user’s iPad or iPhone device. With the latest Xcode build, we will do all this in Swift Programming Language.
iOS devices will be identified in four ways: .phone, .pad, .tv & .unspecified.
So the code will look like this in swift
Code for iOS 13, iPadOS 13, macOS 10.15 Catalyst and above:
if UIDevice.current.userInterfaceIdiom == .phone {
} else if UIDevice.current.userInterfaceIdiom == .pad {
} else if UIDevice.current.userInterfaceIdiom == .carPlay {
} else if UIDevice.current.userInterfaceIdiom == .tv {
} else if UIDevice.current.userInterfaceIdiom == .unspecified {
}
Code for iOS 12 and below:
if UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.pad {
print("It's iPad")
// Your code here
} else if UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.phone {
print("It's iPhone")
// Your code here
} else if UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.tv {
print("It's Apple TV")
// Your code here
} else if UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.unspecified {
print("It's Unknown Device")
// Your code here
}
(more…)