BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

Identify what OS the app is running on

Forums > SwiftUI

I have an app that running in Catalyst and also on iOS.

I want certain features not to be available on MacOS (Catalyst), but I can't seem to identify when the app is running on a Mac. The following code reports the OS as iOS when running on the Mac.

#if os(iOS)
print("OS is iOS")
#elseif os(macOS)
print("OS is MacOS")
#elseif os(watchOS)
print("OS is watchOS")
#elseif os(tvOS)
print("OS is tvOS")
#endif

To some degree I can understand why this is happening, but all said, the reported OS should be MacOS.

Does anyone have an idea as to how I can determine when the app is running on a Mac?

BTW, kinda related but when I use Testflight to distrubute the app, the email invite only mentions iOS (it says something like "Install Testflight for iOS") but clicking the links on a Mac allows you to install Testflight on Mac too. The email is misleading and does not make for a great UX for the beta tester who wants to test the app on a Mac.

Any advice here folks?

Thanks very much!

2      

ProcessInfo can tell you if it is a Catalyst app, it can also get the display version or numeric version of the operating system.

https://developer.apple.com/documentation/foundation/processinfo/3362531-ismaccatalystapp

I'm fairly new to Swift and SwiftUI, but those look like compiler conditionals, which means they get backed in when you build. I believe that you use something like below for runtime conditionals.

if #available( macOS 11.0, * ) { }

Edit 2: To include this potential sample.

if ProcessInfo.processInfo.isMacCatalystApp || ProcessInfo.processInfo.isiOSAppOnMac {}

1      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.