【问题标题】:Running SwiftUI app on macOS, but the platform is recognized as iOS. Why?在 macOS 上运行 SwiftUI 应用程序,但该平台被识别为 iOS。为什么?
【发布时间】:2021-12-04 14:02:25
【问题描述】:

在 macOS 上运行应用,但应用无法将平台识别为 macOS:

#if os(iOS)
        NavigationView {
            let _ = print("platform is iOS")
            ProfileView()
        }
            #else
        NavigationView {
            let _ = print("platform is NOT iOS")
            ProfileView()
        }
// platform is iOS

我是否遗漏了一些关键步骤?

【问题讨论】:

  • 您可能有一个 macCatalyst 应用程序。它们被视为 ios 应用程序。您也可以使用#if targetEnvironment(macCatalyst) 进行检查

标签: swift macos swiftui


【解决方案1】:

解决了这个问题。在 macOS 上运行的应用程序是 macCatalyst 应用程序,因此您必须考虑到这一点:

#if os(OSX)
        NavigationView {
            let _ = print("platform is macOS")
            ProfileView()
        }
        
 #elseif os(iOS)
    

    #if targetEnvironment(macCatalyst)
        NavigationView {
            let _ = print("platform is macCatalyst")
            ProfileView()
        }
            #else
        NavigationView {
            let _ = print("platform is iOS")
            ProfileView()
        }
        #endif
        #endif
// platform is macCatalyst.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-07
    • 2016-02-29
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 2015-11-20
    • 2020-08-07
    • 2015-10-25
    相关资源
    最近更新 更多