ios 在 Swift 中检查操作系统版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24503001/
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
Check OS version in Swift?
提问by Gergely
I'm trying to check system information in Swift. I figured out, that it could be achieved by code:
我正在尝试在 Swift 中检查系统信息。我发现,它可以通过代码实现:
var sysData:CMutablePointer<utsname> = nil
let retVal:CInt = uname(sysData)
I have two problems with this code:
这段代码有两个问题:
- What should be sysData's initial value? This example gives -1 in retVal probably because sysData is nil.
- How can I read information from sysData?
- sysData 的初始值应该是多少?此示例在 retVal 中给出 -1 可能是因为 sysData 为零。
- 如何从 sysData 读取信息?
回答by miho
For iOS, try:
对于 iOS,请尝试:
var systemVersion = UIDevice.current.systemVersion
For OS X, try:
对于 OS X,请尝试:
var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion
If you just want to check if the users is running at least a specific version, you can also use the following Swift 2 feature which works on iOS and OS X:
如果您只想检查用户是否至少运行了特定版本,您还可以使用以下适用于 iOS 和 OS X 的 Swift 2 功能:
if #available(iOS 9.0, *) {
// use the feature only available in iOS 9
// for ex. UIStackView
} else {
// or use some work around
}
BUT it is not recommended to check the OS version. It is better to check if the feature you want to use is available on the device than comparing version numbers.For iOS, as mentioned above, you should check if it responds to a selector; eg.:
但不建议检查操作系统版本。最好检查您要使用的功能是否在设备上可用,而不是比较版本号。对于 iOS,如上所述,您应该检查它是否响应选择器;例如。:
if (self.respondsToSelector(Selector("showViewController"))) {
self.showViewController(vc, sender: self)
} else {
// some work around
}
回答by Aks
Update:
Now you should use new availability checkingintroduced with Swift 2:
e.g. To check for iOS 9.0 or later at compiletime use this:
更新:
现在您应该使用Swift 2 引入的新可用性检查:
例如,要在编译时检查 iOS 9.0 或更高版本,请使用以下命令:
if #available(iOS 9.0, *) {
// use UIStackView
} else {
// show sad face emoji
}
or can be used with whole method or class
或者可以与整个方法或类一起使用
@available(iOS 9.0, *)
func useStackView() {
// use UIStackView
}
For more info see this.
有关更多信息,请参阅此。
Run time checks:
运行时检查:
if you don't want exact version but want to check iOS 9,10 or 11 using if:
如果您不想要确切的版本,但想使用以下条件检查 iOS 9,10 或 11:
let floatVersion = (UIDevice.current.systemVersion as NSString).floatValue
EDIT:Just found another way to achieve this:
编辑:刚刚找到了另一种方法来实现这一目标:
let iOS8 = floor(NSFoundationVersionNumber) > floor(NSFoundationVersionNumber_iOS_7_1)
let iOS7 = floor(NSFoundationVersionNumber) <= floor(NSFoundationVersionNumber_iOS_7_1)
回答by KVISH
I made helper functions that were transferred from the below link into swift:
我制作了从以下链接转移到 swift 的辅助函数:
How can we programmatically detect which iOS version is device running on?
func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame
}
func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending
}
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending
}
func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending
}
func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedDescending
}
It can be used like so:
它可以像这样使用:
SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO("7.0")
Swift 4.2
斯威夫特 4.2
func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedSame
}
func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedDescending
}
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != .orderedAscending
}
func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedAscending
}
func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != .orderedDescending
}
回答by nahung89
Swift 5
斯威夫特 5
We dont need to create extension since ProcessInfo
gives us the version info. You can see sample code for iOS as below.
我们不需要创建扩展,因为ProcessInfo
给了我们版本信息。您可以看到如下所示的 iOS 示例代码。
let os = ProcessInfo().operatingSystemVersion
switch (os.majorVersion, os.minorVersion, os.patchVersion) {
case (let x, _, _) where x < 8:
print("iOS < 8.0.0"
case (8, 0, _):
print("iOS >= 8.0.0, < 8.1.0")
case (8, _, _):
print("iOS >= 8.1.0, < 9.0")
case (9, _, _):
print("iOS >= 9.0.0")
default:
print("iOS >= 10.0.0")
}
Reference: http://nshipster.com/swift-system-version-checking/
回答by YannSteph
I made this Singleton for simple use, created an IOSVersion.swift
file and added this code :
我为了简单使用而制作了这个单例,创建了一个IOSVersion.swift
文件并添加了以下代码:
import UIKit
public class IOSVersion {
class func SYSTEM_VERSION_EQUAL_TO(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame
}
class func SYSTEM_VERSION_GREATER_THAN(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version as String,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending
}
class func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version as String,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending
}
class func SYSTEM_VERSION_LESS_THAN(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version as String,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending
}
class func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version as String,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedDescending
}
}
USE :
用 :
IOSVersion.SYSTEM_VERSION_EQUAL_TO("8.0")
IOSVersion.SYSTEM_VERSION_LESS_THAN("8.0")
Thanks @KVISH
谢谢@KVISH
Edit Swift 2 :
编辑斯威夫特 2 :
if #available(iOS 9.0, *) {
//
} else {
//
}
回答by neoneye
Swift 5
斯威夫特 5
func run() {
let version = OperatingSystemVersion(majorVersion: 13, minorVersion: 0, patchVersion: 0)
if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) {
runNewCode()
} else {
runLegacyCode()
}
}
func runNewCode() {
guard #available(iOS 13.0, *) else {
fatalError()
}
// do new stuff
}
func runLegacyCode() {
// do old stuff
}
回答by FreeNickname
If you're using Swift 2and you want to check the OS version to use a certain API, you can use new availability feature:
如果您使用的是Swift 2并且想要检查操作系统版本以使用某个 API,您可以使用新的可用性功能:
if #available(iOS 8, *) {
//iOS 8+ code here.
}
else {
//Code for iOS 7 and older versions.
//An important note: if you use #availability, Xcode will also
//check that you don't use anything that was introduced in iOS 8+
//inside this `else` block. So, if you try to use UIAlertController
//here, for instance, it won't compile. And it's great.
}
I wrote this answer because it is the first question in Google for the swift 2 check system version
query.
我写这个答案是因为它是谷歌swift 2 check system version
查询的第一个问题。
回答by Mellong Lau
Update for Swift 3.0+
Swift 3.0+ 更新
func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == ComparisonResult.orderedSame
}
func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == ComparisonResult.orderedDescending
}
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != ComparisonResult.orderedAscending
}
func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == ComparisonResult.orderedAscending
}
func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != ComparisonResult.orderedDescending
}
回答by Vasily Bodnarchuk
Details
细节
- Xcode 10.2.1 (10E1001), Swift 5
- Xcode 10.2.1 (10E1001),Swift 5
Links
链接
Solution
解决方案
extension OperatingSystemVersion {
func getFullVersion(separator: String = ".") -> String {
return "\(majorVersion)\(separator)\(minorVersion)\(separator)\(patchVersion)"
}
}
let os = ProcessInfo().operatingSystemVersion
print(os.majorVersion) // 12
print(os.minorVersion) // 2
print(os.patchVersion) // 0
print(os.getFullVersion()) // 12.2.0
回答by Tengai
The easiest and the simplest way to check system version (and a lot of other versions) in Swift 2 and higher is:
在 Swift 2 及更高版本中检查系统版本(以及许多其他版本)的最简单和最简单的方法是:
if #available(iOS 9.0, *) { // check for iOS 9.0 and later
}
Also, with #available
you can check versions of these:
此外,#available
您可以检查这些版本:
iOS
iOSApplicationExtension
macOS
macOSApplicationExtension
watchOS
watchOSApplicationExtension
tvOS
tvOSApplicationExtension
swift