Programmatically detect if iOS app is running on a Mac.

Programmatically detect if iOS app is running on a Mac,
is that possible?
Unmodified iOS apps running on a Mac with Apple Silicon do not need to do any compatibility checking. What problem are you trying to solve?
My app is an audio player that has a MPVolumeView. As far as I can tell, the MPVolumeView isn't available when running on a Mac? Is it? At least it's not visible.
So that space is empty and in that case I need to use that space for other purposes. And I can't come up with a way to find out if it's displayed or not unless I know if the app is running on a Mac or not.
Maybe I'm wrong?

Anyone?
I reported the problem with MPVolumeView to Apple using theFeedback assistant, and here is the reply:

"After reviewing your feedback, we have some additional information for you:
This is not ARM specific; it’s how MPVolumeView works on Mac. It’s not supported."

OK, if it's not supported then it should't be possible to create a MPVolumeView but it is.
The one need to find out if an iOS app is running on a Mac so that the space occupied by the MPVolumeView can be used for something else, but how?


Here is one way to check if running on macOS or iOS (plain C code)

BOOL IsAppRunningOnMac()
{

  /*
path to the "Documents" folder on iOS:
  "/var/mobile/Containers/Data/Application/8C2D631A-DCBB-44FE-8F86-429A89FCE921/Documents" -> iOS

path to the "Documents" folder on macOS:
  "/Users/USER_NAME/Library/Containers/4EE76C41-2BE8-4A65-B26C-080773E0EB31/Data/Documents"  -> Mac
  */

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

  // this code might break at some point!
  if (![documentsDirectory hasPrefix:@"/var/mobile/"] && ![documentsDirectory hasPrefix:@"/Users/"])
    {
    // Check that Apple hasn't changed something
    assert(0);  // development: notify us by asserting
    return NO;  // production: assume iOS
    }

  return [documentsDirectory hasPrefix:@"/Users/"];

}

Take a look at NSProcessInfo, as it has a property you can look at for this purpose.
You could try this! This is what I did to help my catalyst app figure out if it was on iPhone, iPad, or Mac
Hope this is helpful.

#if targetEnvironment(macCatalyst)
            print("Device: macCatalyst")
#else
        if UIDevice.current.model == "iPad" {

                print("Device: iPad")

          }else{
                print("Device: iPhone")

          }
#endif
Hello guys !
I'm interested in this matter, Indeed like @edford said with Mac Apple Silicon the app should not need any modification.
But what if I do want to make some things different when I'm on a mac !?
Is the only solution using Mac Catalyst ? or can I test if i'm "running my iOS app on Mac os" somehow?



Actually the answer was on edford's answer, just use NSProcessInfo :

Code Block
if (@available(iOS 14.0, *)) {
if ([NSProcessInfo processInfo].isiOSAppOnMac) {
NSLog(@"I'M ON MAC");
}else{
NSLog(@"IM NOT ON MAC");
}
}

One of these methods should work.

Code Block objc
- (BOOL)isiOSAppOnMac {
 if (@available(iOS 14.0, *)) {
   return NSProcessInfo.processInfo.isiOSAppOnMac;
 }
  
 return false;
}


Code Block swift
func isiOSAppOnMac() -> Bool {
if #available(iOS 14.0, *) {
return ProcessInfo.processInfo.isiOSAppOnMac
}
return false
}


There is at least one compatibility issue currently - I found that using UIReferenceLibraryViewController crashes unexpectedly on an iOS app running on a Mac with

*** Assertion failure in void *DictionaryServicesLibrary(void)(), _UIDictionaryManager.m:25

I ended up adding a ProcessInfo check to avoid this.

ProcessInfo.processInfo.isMacCatalystApp returns true for:

  1. Mac app built with Mac Catalyst, or
  2. iOS app running on Apple silicon

If you specifically wants to know for (2), then use ProcessInfo.processInfo.isiOSAppOnMac

Now you can simply use this:

if ([NSProcessInfo processInfo].isiOSAppOnMac) {

NSLog(@"iOS app is running on MAC");

}

Programmatically detect if iOS app is running on a Mac.
 
 
Q