I am trying to develop an app that runs on iMacs, iPhones and iPads. so how, using SwiftUI, do I check what device I am running on?
the View Controllers work differently on a Mac than on an iPad/iPhone.
In that case userInterfaceIdiom
is definitely the good option.
Earlier you wrote:
and
UIDevice
is part of UIKit which won't work on macOS. I think I need a SwiftUI solution.
SwiftUI is a UI framework. While it works on all Apple’s platforms, it doesn’t do everything. Sometimes you need to work with platform-specific APIs.
In this case it depends on how you’re building your macOS app:
-
If you’re running your iOS app unmodified (iOS Apps on Mac), there’s the
isiOSAppOnMac
property onProcessInfo
. -
If you’re using Mac Catalyst,
userInterfaceIdiom
is the answer. -
If you’re building a cross platform app, you don’t have access to
UIDevice
in your macOS build but that’s OK because the macOS build only runs on macOS. So, you can use a compile-time check, for example:
let isBuiltForMacOS: Bool = {
#if os(macOS)
return true
#else
return false
#endif
}()
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"