如何从 Swift 确定设备类型?(OS X 或 iOS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24065017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:32:25  来源:igfitidea点击:

How to determine device type from Swift? (OS X or iOS)

iosmacosswift

提问by user1947561

I know Swift is relatively new, but I was wondering if there was a way to determine the device type?

我知道 Swift 相对较新,但我想知道是否有办法确定设备类型?

(Like you used to be able to do with a #define)?

(就像你以前可以用 a 做的那样#define)?

Mainly I would like to know how to differentiate OS X or iOS. I have found nothing on the subject.

我主要想知道如何区分 OS X 或 iOS。我在这个主题上什么也没找到。

回答by rickster

If you're building for both iOS and OS X (and maybe now for watchOS and tvOS, too), you're building your code at least twice: once for each platform. If you want different code to execute on each platform, you want a build-time conditional, not a run-time check.

如果您正在为 iOS 和 OS X(也许现在也为 watchOS 和 tvOS)构建代码,那么您至少要构建两次代码:每个平台一次。如果希望在每个平台上执行不同的代码,则需要构建时条件检查,而不是运行时检查。

Swift has no preprocessor, but it does have conditional build directives — and for the most part, they look like the C equivalent.

Swift 没有预处理器,但它有条件构建指令——而且在大多数情况下,它们看起来像 C 等价物。

#if os(iOS) || os(watchOS) || os(tvOS)
    let color = UIColor.redColor()
#elseif os(OSX)
    let color = NSColor.redColor()
#else
    println("OMG, it's that mythical new Apple product!!!")
#endif

You can also use build configurations to test for architecture (x86_64, arm, arm64, i386) or -Dcompiler flags (including the DEBUGflag defined by the standard Xcode templates).

您还可以使用构建配置来测试架构(x86_64armarm64i386)或-D编译器标志(包括DEBUG由标准 Xcode 模板定义的标志)。

See Preprocessor Directivesin Using Swift with Cocoa and Objective-C.

请参阅在Cocoa 和 Objective-C使用 Swift 中的预处理器指令

(If you want to distinguish which kind of iOS device you're on at runtime, use the UIDeviceclass just like you would from ObjC. It's typically more useful and safe to look at the device attributes that are important to you rather than a device name or idiom — e.g. use traits and size classes to lay out your UI, query OpenGL for the GPU capabilities you require, etc.)

(如果您想在运行时区分您使用的是哪种 iOS 设备,请UIDevice像使用ObjC 一样使用该类。查看对您很重要的设备属性而不是设备名称通常更有用和更安全或习惯用法——例如使用特征和大小类来布置您的 UI,查询 OpenGL 以获得您需要的 GPU 功能等)

回答by Adam Foot

This should provide you with every use case:

这应该为您提供每个用例:

#if os(OSX)
    print("macOS")
#elseif os(watchOS)
    print("watchOS")
#elseif os(tvOS)
    print("tvOS")
#elseif os(iOS)
    #if targetEnvironment(macCatalyst)
        print("macOS - Catalyst")
    #else
        print("iOS")
    #endif
#endif

回答by raed

Since Swift 4.2 you can replace

从 Swift 4.2 开始,您可以替换

#if os(iOS) || os(watchOS) || os(tvOS)
    let color = UIColor.redColor()
#elseif os(OSX)
    let color = NSColor.redColor()
#else
     println("OMG, it's that mythical new Apple product!!!")
#endif

By

经过

#if canImport(UIKit)
    let color = UIColor.redColor()
#elseif os(OSX)
    let color = NSColor.redColor()
#else
    #error("OMG, it's that mythical new Apple product!!!")
#endif

回答by user12465607

Updating for Mac Catalyst. You may also now use the following to determine if iOS or Mac Catalyst:

更新 Mac Catalyst。您现在还可以使用以下内容来确定是 iOS 还是 Mac Catalyst:

let color: UIColor
#if targetEnvironment(macCatalyst)
color = .systemRed
#else
color = .systemBlue
#endif

For example.

例如。

回答by A.G

var device = UIDevice.currentDevice().model 

This code worked for me. I have implemented that on textfield and keyboard dismissing part. See below.

这段代码对我有用。我已经在文本字段和键盘关闭部分实现了这一点。见下文。

func textFieldShouldBeginEditing(textField: UITextField) -> Bool
{

    print(device)

    if (textField.tag  == 1 && (device == "iPhone" || device == "iPhone Simulator" ))
    {
        var scrollPoint:CGPoint = CGPointMake(0,passwordTF.frame.origin.y/2);
        LoginScroll!.setContentOffset(scrollPoint, animated: true);
    }
    else if (textField.tag  == 2 && (device == "iPhone" || device == "iPhone Simulator"))
    {
        var scrollPoint:CGPoint = CGPointMake(0,passwordTF.frame.origin.y/1.3);
        LoginScroll!.setContentOffset(scrollPoint, animated: true);
    }

    return true

}