简体   繁体   English

有没有办法检测 Xcode iOS/MacOS 多平台应用程序中运行的是哪个平台?

[英]Is there a way to detect which platform is running in an Xcode iOS/MacOS Multiplatform app?

I'm working on an app with Xcode 14.0 beta 3 (14A5270f).我正在使用 Xcode 14.0 beta 3 (14A5270f) 开发一个应用程序。 The target Supported Destinations are:支持的目标是:

  • iPhone (iOS) iPhone (iOS)
  • iPad (iOS) iPad (iOS)
  • Mac (macOS) Mac (macOS)

iOS Deployment Target is 16.0 macOS Deployment Target is 13.0 iOS 部署目标是 16.0 macOS 部署目标是 13.0

I'm looking for some way to detect which destination is running.我正在寻找某种方法来检测正在运行的目的地。 I have a barcode scanning view that I only want to show on the iPhone or iPad destinations.我有一个条码扫描视图,我只想在 iPhone 或 iPad 目的地上显示。

Thanks to https://stackoverflow.com/users/341994/matt for the documentation pointer in the comments that got me going.感谢https://stackoverflow.com/users/341994/matt在让我前进的评论中提供的文档指针。 Here's my final solution.这是我的最终解决方案。

enum PlatformDestination: Int {
    case iPhone
    case iPad
    case iOSOnMac
    case MacCatalyst
    case Mac

    static func destination() -> PlatformDestination {
        #if os(iOS)
        switch UIDevice.current.userInterfaceIdiom {
        case .phone:
            return .iPhone
        case .pad:
            return .iPad
        case .carPlay:
            return .CarPlay
        case .tv:
            return .tvOS
        case .unspecified:
            break
        case .mac:
            return .iOSOnMac
        default:
            break
        }
        #endif
        if ProcessInfo().isMacCatalystApp {
            return .MacCatalyst
        } else {
            return .Mac
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在带有过时版本的Xcode和macOS的iOS 12.2上运行应用程序? - Is there a way to run an app on iOS 12.2 with an outdated version of Xcode and macOS? 当我的应用程序在ios中处于后台时,有没有办法检测正在运行的应用程序? - Is there a way to detect the running app, while my app is in the background in ios? 使用sysctl检测当前在iOS上运行的应用程序 - Detect which app is currently running on iOS using sysctl Flutter App 不在 iOS 平台运行 - Flutter App is not running in the iOS platform Flutter 应用程序未在 iOS 平台上运行 - Flutter app not running with iOS platform 在同一个 Xcode 项目中拥有 iOS 和 macOS 应用程序 - Having iOS and macOS app in same Xcode project 在 macOS 上运行多平台 SwiftUI 应用程序时,是否可以跳过 launchscreen.storyboard 文件? - Is it possible to skip launchscreen.storyboard file when running a multiplatform SwiftUI app on macOS? 有没有办法在iOS中检测正在运行的动画? - Is there a way to detect a running animation in iOS? 检测app是否在iOS测试版上运行? - Detect if app is running on iOS beta? SwiftUI:多平台应用程序 swiftui 意外的平台条件 - SwiftUI: Multiplatform app swiftui unexpected platform condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM