TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.