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

SOLVED: Detecting if app is running on MacOS

Forums > SwiftUI

Hi,

I'm starting to get my app ready to run under Mac Catalyst. Thing is there are certain user interface elements I do not want to display when the app is running on MacOS.

Can anyone advise, how do I detect if the app is running on MacOS. I've tried the example below but when I run the app on the Mac (designed for iPad build target) I do not see Macos displayed and the code in 'code here' runs.

Any ideas?

#if os(macOS)
Text("Macos")
#else
Text("ios")
... code here
....

}
#endif

I also tried below. When I run this in Mac Catalyst I see "Running on iOS"

#if targetEnvironment(macCatalyst)
        Text("Running on macOS Catalyst")
        #else
        Text("Running on iOS")
        #endif

2      

I think Mac Catalyst runs iOS not macOS. If you look at Target>General>Supported Destinations you will see that SDK for Mac (Designed for iPAd) is iOS

The idea is that iPad app can run on a Mac.

If you have thing that you do not want to see when running on Macs then I would add new target and build for the Mac.

2      

What Nigel said; macOS is a whole 'nother thing.

The good news is that most of it will be able to be cross-platform.

2      

thanks guys!

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.