预处理指令以检测是否在 macOS 上运行,使用 “Designed for iPad” 目标。

huangapple go评论65阅读模式
英文:

Preprocessor directive to detect running on macOS using the Designed for iPad target

问题

Apple提供了ProcessInfo的运行时属性isiOSAppOnMac,用于检测是否在Mac上运行,并使用Designed for iPad目标。

我更愿意使用预处理器指令,而不是进行运行时检查。在使用MacCatalyst目标时,可以这样做:

#if targetEnvironment(macCatalyst)

我尝试过以下两种方式:

#if os(macOS)
#if !os(iOS)

但这些不适用于Designed for iPad。以下方式似乎可以工作,但我想知道是否有更好的方法?

#if arch(arm64) && os(iOS) && !targetEnvironment(simulator)
    print("Designed for iPad on a Mac")
#endif
英文:

Apple provides a runtime isiOSAppOnMac property of ProcessInfo to detect if running on a Mac using the Designed for iPad target.

I'd much rather use a preprocessor directive than having to do runtime checks. When using a MacCatalyst target this works:

#if targetEnvironment(macCatalyst)

I've tried both:

#if os(macOS)
#if !os(iOS)

But those do not work for Designed for iPad. This actually seems to work, but I was wondering if there is a better way?

#if arch(arm64) && os(iOS) && !targetEnvironment(simulator)
    print("Designed for iPad on a Mac")
#endif

答案1

得分: 0

请记住,在这种情况下,您不需要为Mac版本的应用程序执行单独的编译。这只是iPad应用程序在Apple Silicon Mac上运行的问题。由于只有一个iOS构建,您不能使用编译器标志。您唯一的选择是使用运行时检查来确定iPad应用程序当前是否在Mac上运行。

当然,如果您稍后构建了Mac版本的应用程序,使用Mac Catalyst(以便支持除Apple Silicon Mac之外的Intel Mac),那么您可以使用编译器标志(正如您在问题中指出的那样),因为您有两个构建,一个用于Mac(通过Mac Catalyst),一个用于iOS。

英文:

Keep in mind that you do not perform a separate compilation for a Mac version of the app in this case. It's simply a matter of the iPad app running on an Apple Silicon Mac. Since there's only the one iOS build, you can't use compiler flags. Your only option is to use runtime checks to determine if the iPad app is currently running on a Mac.

Of course if you later build a Mac version of the app using Mac Catalyst (so you can support Intel Macs in addition to Apple Silicon Macs), then you can use compiler flags (as you pointed out in the question) since you have two builds, one for the Mac (via Mac Catalyst), and one for iOS.

huangapple
  • 本文由 发表于 2023年6月2日 06:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定