Programmatically detect if iOS app is running on a Mac.

Programmatically detect if iOS app is running on a Mac,
is that possible?
Post not yet marked as solved Up vote post of florom Down vote post of florom
10k views

Replies

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?

  • Get a window instance and compare:

    window.bounds.size.width VS UIScreen.main.bounds.size.width

Add a Comment
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/"];

}

  • These paths are subject to change at a moments notice on iOS Id think. but Debian file directorys probably won't change for a long time. but this is a clever solution. I might consider this a hack, because reasons. like I said, im not sure if they ever change this FS tree.

  • simulator

    /Users/USER/Library/Developer/CoreSimulator/Devices/C065C350-8DC0-4CA5-AF63-A19D998B41DF/data/Containers/Data/Application/5A45A272-AD16-4F1C-939F-BCED22E4A7B4/Documents

    Mac apple silicon

    /Users/USER/Library/Containers/43B15322-8173-4D40-BE9A-D7539EFC41A7/Data/Documents"

Add a Comment
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

  • How can I test my iOS app to run on Apple silicon. Is it possible from test flight?

Add a Comment

Now you can simply use this:

if ([NSProcessInfo processInfo].isiOSAppOnMac) {

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

}