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

时间:2023-01-24 09:04:30

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)?

(就像你以前使用#define一样)?

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

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

4 个解决方案

#1


50  

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 -D compiler flags (including the DEBUG flag defined by the standard Xcode templates).

您还可以使用构建配置来测试体系结构(x86_64、arm、arm64、i386)或-D编译器标志(包括由标准Xcode模板定义的调试标志)。

See Preprocessor Directives in Using Swift with Cocoa and Objective-C.

参见使用Swift与Cocoa和Objective-C中的预处理器指令。

(If you want to distinguish which kind of iOS device you're on at runtime, use the UIDevice class 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设备,可以像使用ObjC一样使用UIDevice类。通常,查看对您来说重要的设备属性比查看设备名称或习惯用法更有用和安全——例如,使用trait和size类来布置UI,查询OpenGL以获得所需的GPU功能,等等。

#2


0  

I've implemented a super-lightweight library to detect the used device: https://github.com/schickling/Device.swift

我实现了一个超轻量级的库来检测使用的设备:https://github.com/schickling/Device.swift

It can be installed via Carthage and be used like this:

它可以通过Carthage安装,使用方式如下:

import Device

let deviceType = UIDevice.currentDevice().deviceType

switch deviceType {
case .IPhone6: print("Do stuff for iPhone6")
case .IPadMini: print("Do stuff for iPad mini")
default: print("Check other available cases of DeviceType")
}

#3


0  

var device = UIDevice.currentDevice().model 

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

这段代码对我有用。我已经在textfield和键盘取消部分实现了这一点。见下文。

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

}

#4


0  

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

#1


50  

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 -D compiler flags (including the DEBUG flag defined by the standard Xcode templates).

您还可以使用构建配置来测试体系结构(x86_64、arm、arm64、i386)或-D编译器标志(包括由标准Xcode模板定义的调试标志)。

See Preprocessor Directives in Using Swift with Cocoa and Objective-C.

参见使用Swift与Cocoa和Objective-C中的预处理器指令。

(If you want to distinguish which kind of iOS device you're on at runtime, use the UIDevice class 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设备,可以像使用ObjC一样使用UIDevice类。通常,查看对您来说重要的设备属性比查看设备名称或习惯用法更有用和安全——例如,使用trait和size类来布置UI,查询OpenGL以获得所需的GPU功能,等等。

#2


0  

I've implemented a super-lightweight library to detect the used device: https://github.com/schickling/Device.swift

我实现了一个超轻量级的库来检测使用的设备:https://github.com/schickling/Device.swift

It can be installed via Carthage and be used like this:

它可以通过Carthage安装,使用方式如下:

import Device

let deviceType = UIDevice.currentDevice().deviceType

switch deviceType {
case .IPhone6: print("Do stuff for iPhone6")
case .IPadMini: print("Do stuff for iPad mini")
default: print("Check other available cases of DeviceType")
}

#3


0  

var device = UIDevice.currentDevice().model 

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

这段代码对我有用。我已经在textfield和键盘取消部分实现了这一点。见下文。

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

}

#4


0  

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