我正在尝试检查Swift中的系统信息.我想通过代码可以实现:
var sysData:CMutablePointer= nil let retVal:CInt = uname(sysData)
这个代码有两个问题:
什么应该是sysData的初始值?此示例在retVal中给出-1可能是因为sysData为nil.
如何从sysData中读取信息?
miho.. 344
对于iOS,请尝试:
var systemVersion = UIDevice.current.systemVersion
对于OS X,请尝试:
var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion
如果您只想检查用户是否至少运行了特定版本,您还可以使用以下适用于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 }
但是不建议检查操作系统版本.最好检查设备上是否有可用的功能,而不是比较版本号. 对于iOS,如上所述,您应该检查它是否响应选择器; 例如.:
if (self.respondsToSelector(Selector("showViewController"))) { self.showViewController(vc, sender: self) } else { // some work around }
Aks.. 79
更新:
现在您应该使用Swift 2引入的新可用性检查:
例如,要在编译时检查iOS 9.0或更高版本,请使用以下命令:
if #available(iOS 9.0, *) { // use UIStackView } else { // show sad face emoji }
或者可以与整个方法或类一起使用
@available(iOS 9.0, *) func useStackView() { // use UIStackView }
欲了解更多信息请参阅本.
运行时检查:
如果您不想要确切的版本但想要使用if检查iOS 9,10或11:
let floatVersion = (UIDevice.current.systemVersion as NSString).floatValue
编辑: 刚刚找到另一种方法来实现这一目标:
let iOS8 = floor(NSFoundationVersionNumber) > floor(NSFoundationVersionNumber_iOS_7_1) let iOS7 = floor(NSFoundationVersionNumber) <= floor(NSFoundationVersionNumber_iOS_7_1)
KVISH.. 48
我创建了从以下链接转移到swift的辅助函数:
我们如何以编程方式检测运行设备的iOS版本?
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 }
它可以像这样使用:
SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO("7.0")
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
}
nahung89.. 27
Swift 3.0
我们不需要创建扩展,因为ProcessInfo
我们提供了版本信息.您可以看到iOS的示例代码,如下所示.
let os = ProcessInfo().operatingSystemVersion switch (os.majorVersion, os.minorVersion, os.patchVersion) { 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: // this code will have already crashed on iOS 7, so >= iOS 10.0 println("iOS >= 10.0.0") }
参考:http://nshipster.com/swift-system-version-checking/
我创建了从以下链接转移到swift的辅助函数:
我们如何以编程方式检测运行设备的iOS版本?
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 }
它可以像这样使用:
SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO("7.0")
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
}
我们不需要创建扩展,因为ProcessInfo
我们提供了版本信息.您可以看到iOS的示例代码,如下所示.
let os = ProcessInfo().operatingSystemVersion switch (os.majorVersion, os.minorVersion, os.patchVersion) { 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: // this code will have already crashed on iOS 7, so >= iOS 10.0 println("iOS >= 10.0.0") }
参考:http://nshipster.com/swift-system-version-checking/
func run() { let version = OperatingSystemVersion(majorVersion: 11, minorVersion: 0, patchVersion: 0) if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) { runNewCode() } else { runLegacyCode() } } func runNewCode() { guard #available(iOS 11.0, *) else { fatalError() } // do new stuff } func runLegacyCode() { // do old stuff }
我使这个Singleton用于简单使用,创建了一个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 } }
使用 :
IOSVersion.SYSTEM_VERSION_EQUAL_TO("8.0") IOSVersion.SYSTEM_VERSION_LESS_THAN("8.0")
谢谢@KVISH
编辑Swift 2:
if #available(iOS 9.0, *) { // } else { // }
更新:
现在您应该使用Swift 2引入的新可用性检查:
例如,要在编译时检查iOS 9.0或更高版本,请使用以下命令:
if #available(iOS 9.0, *) { // use UIStackView } else { // show sad face emoji }
或者可以与整个方法或类一起使用
@available(iOS 9.0, *) func useStackView() { // use UIStackView }
欲了解更多信息请参阅本.
运行时检查:
如果您不想要确切的版本但想要使用if检查iOS 9,10或11:
let floatVersion = (UIDevice.current.systemVersion as NSString).floatValue
编辑: 刚刚找到另一种方法来实现这一目标:
let iOS8 = floor(NSFoundationVersionNumber) > floor(NSFoundationVersionNumber_iOS_7_1) let iOS7 = floor(NSFoundationVersionNumber) <= floor(NSFoundationVersionNumber_iOS_7_1)
如果您使用的是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. }
我写了这个答案,因为这是Google在swift 2 check system version
查询中的第一个问题.
对于iOS,请尝试:
var systemVersion = UIDevice.current.systemVersion
对于OS X,请尝试:
var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion
如果您只想检查用户是否至少运行了特定版本,您还可以使用以下适用于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 }
但是不建议检查操作系统版本.最好检查设备上是否有可用的功能,而不是比较版本号. 对于iOS,如上所述,您应该检查它是否响应选择器; 例如.:
if (self.respondsToSelector(Selector("showViewController"))) { self.showViewController(vc, sender: self) } else { // some work around }
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 }