Determine If iOS Code Is Running On MacOS

How do you determine if your iOS app is running on MacOS?

Currently, I am using the following code:

    var weHaveMac = false    
    if #available(iOS 14.0, *) {
        if  UIDevice.current.userInterfaceIdiom == .mac {
            weHaveMac = true
            print ("Mac?: \(weHaveMac), system?: \(UIDevice.current.userInterfaceIdiom)")
        }
    }

But, the UIDevice.current.userInterfaceIdiom == .Mac test always returns false.

I am using Xcode 12.2 Beta with MacOS 11.0.1 Beta.

Answered by DTS Engineer in 646106022
userInterfaceIdiom only returns Mac when the app opts into using the Mac idiom as a Mac Catalyst app. This situation is discussed in Adapting iOS Code to Run in the macOS Environment.
it is .mac, not .Mac
So code seems OK, but comment below is not.

What do you get from your print statement ?

Could you print the UIDevice.current.userInterfaceIdiom

Code Block
print("interface", UIDevice.current.userInterfaceIdiom)
    if UIDevice.current.userInterfaceIdiom == .mac {
      weHaveMac = true
      print ("Mac?: \(weHaveMac), system?: \(UIDevice.current.userInterfaceIdiom)")
    }


When you test   if #available(iOS 14.0, *)
on a Mac, it fails most likely

You should probably test like:
Code Block
if #available(iOS 14.0, *), #available(macOS 11.0, *) {


Accepted Answer
userInterfaceIdiom only returns Mac when the app opts into using the Mac idiom as a Mac Catalyst app. This situation is discussed in Adapting iOS Code to Run in the macOS Environment.
Determine If iOS Code Is Running On MacOS
 
 
Q