Search code examples
swiftmac-catalyst

Is there a way to detect if an app is "Scaled to Match iPad" versus "Optimize for Mac"


Is there a way to detect either at runtime or compile time if a Mac Catalyst app is "Scaled to Match iPad" versus "Optimize for Mac".

The Conditional Compilation Block suggests this info is not available at compilation time with a method similar to targetEnvironment(macCatalyst).

The Build settings reference says this info is in the TARGETED_DEVICE_FAMILY Info.plist entry, so perhaps I can get it from there at runtime.

This info isn't present in ProcessInfo.processInfo.environment.


Solution

  • There is no compile-time check available. If you look at the build log when compiling a Swift file, there is no difference in the flags passed to the compiler between the two settings.

    But as described in Choosing a user interface idiom for your Mac app, there is runtime check. This involves looking at the interface idiom from an appropriate trait collection or from UIDevice userInterfaceIdiom.

    When you build as "Optimize for Mac" then you get the .mac idiom. When you build as "Scaled to Match iPad" then you get the .ipad idiom.

    An example shown in the linked documentation is:

    let childViewController: UIViewController
    if traitCollection.userInterfaceIdiom == .mac {
        childViewController = MacOptimizedChildViewController()
    } else {
        childViewController = ChildViewController()
    }
    

    There may be cases when you will want to wrap such code in a compiler check for targetEnvironment(macCatalyst) so the iOS code can use a different option from either Mac version.