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?
SwiftUI How can dertine what device I am running
UIDevice.current.userInterfaceIdiom == .pad means an iPad, .phone means an iPhone. You can use UIDevice.current.systemName to get the operating system in use. See here.
Just use UIDevice.current.localizedModel
var body: some View {
HStack {
Text("Device Type: ")
Text(UIDevice.current.localizedModel)
}
}
It’s hard to answer this without more context. Why do you need this info? What decisions do you plan to make based on it?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Hi Quinn,
As a diabetic, I am trying to develop an App that will help monitor my Blood Glucose and compare it with my food intake, and my physical activity. I am using SwiftData and Cloudkit to share the data across all my devices. I want the App to have a background Image and a logo. and the View Controllers work differently on a Mac than on an iPad/iPhone.
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
UIDeviceis 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
isiOSAppOnMacproperty onProcessInfo. -
If you’re using Mac Catalyst,
userInterfaceIdiomis the answer. -
If you’re building a cross platform app, you don’t have access to
UIDevicein 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"