iosswiftmacospreprocessor-directive

Preprocessor directive to detect running on macOS using the Designed for iPad target


Apple provides a runtime isiOSAppOnMac property of ProcessInfo to detect if running on a Mac using the Designed for iPad target.

I'd much rather use a preprocessor directive than having to do runtime checks. When using a MacCatalyst target this works:

#if targetEnvironment(macCatalyst)

I've tried both:

#if os(macOS)
#if !os(iOS)

But those do not work for Designed for iPad. This actually seems to work, but I was wondering if there is a better way?

#if arch(arm64) && os(iOS) && !targetEnvironment(simulator)
    print("Designed for iPad on a Mac")
#endif

Solution

  • Keep in mind that you do not perform a separate compilation for a Mac version of the app in this case. It's simply a matter of the iPad app running on an Apple Silicon Mac. Since there's only the one iOS build, you can't use compiler flags. Your only option is to use runtime checks to determine if the iPad app is currently running on a Mac.

    Of course if you later build a Mac version of the app using Mac Catalyst (so you can support Intel Macs in addition to Apple Silicon Macs), then you can use compiler flags (as you pointed out in the question) since you have two builds, one for the Mac (via Mac Catalyst), and one for iOS.