How to detect “Designed for iPad” Mac apps in your Swift code
With the introduction of Apple Silicon Macs, a new device destination called Mac (Designed for iPad) became available within XCode. This destination allows developers to distribute their iOS apps directly to Mac computers without the need for adjustments or dealing with the overhead and issues associated with Mac Catalyst apps.
If an iPad app is already released in the app store it will automatically become available for Apple Silicon Macs per default. Since Apple Silicon uses the same ARM CPU architecture as iPhones and iPads, these machines can run mobile apps natively.
However, this option also makes traditional checks for the system on which the app is running unreliable. As the destination’s title suggests, apps run essentially as iPad apps.
Platform specific checks
Occasionally, you may need to execute code exclusively on certain systems, or you might wish to utilize features such as the EditButton, which is unavailable natively on Mac. In such cases, it’s necessary to conditionally compile your code to exclude it from that system.
Imagine you have checks for the underlying system in your code like:
#if os(iOS)
Text("Hello from iOS")
#elseif os(macOS)
Text("Hello from macOS")
#elseif targetEnvironment(macCatalyst)
Text("Hello from Catalyst")
#endif
If you run this code on an iPhone or iPad, the text ‘Hello from iOS’ will be visible, just as expected. Running it with Mac as the destination, the text ‘Hello from macOS’ will appear.
However, if you select Mac (Designed for iPad) as the destination, you will still see the text ‘Hello from iOS’ – which may be unexpected since the code is running on a Mac.
The issue arises from the fact that although your app operates on a Mac, it doesn’t entirely. When you choose Mac (Designed for iPad) as the destination, your code executes natively on a Mac. However, it exclusively runs the iPad version of the app, which internally identifies as iOS.
How to check for Designed for iPad
Fortunately, determining the underlying system is straightforward with the help of the ProcessInfo class.
This class offers a boolean variable that identifies the system on which the app is running and with that you can use code like:
if ProcessInfo.processInfo.isiOSAppOnMac {
Text("Hello from macOS")
}
This code will only show the text on Macs with the destination set to Designed for iPad and not on mobile devices like iPhones or iPads.
With this class you can also check if the app runs as a Catalyst app:
if ProcessInfo.processInfo.isMacCatalystApp {
Text("Hello from Catalyst")
}
The class has much more to offer, and I recommend you have a look at the documentation.
For those specifically seeking to check for the Designed for iPad destination, this information should resolve their issue.