我正在用Swift检查系统信息。我发现,这可以通过代码来实现:
var sysData:CMutablePointer<utsname> = nil
let retVal:CInt = uname(sysData)
这段代码有两个问题:
sysData的初始值应该是什么?这个例子在retVal中给出-1可能是因为sysData为nil。 如何从sysData读取信息?
我正在用Swift检查系统信息。我发现,这可以通过代码来实现:
var sysData:CMutablePointer<utsname> = nil
let retVal:CInt = uname(sysData)
这段代码有两个问题:
sysData的初始值应该是什么?这个例子在retVal中给出-1可能是因为sysData为nil。 如何从sysData读取信息?
当前回答
注:iOS 8.0及以上版本支持。OS X v10.10及以上版本
var majorVersion: Int { return NSProcessInfo.processInfo().operatingSystemVersion.majorVersion }
var minorVersion: Int { return NSProcessInfo.processInfo().operatingSystemVersion.minorVersion }
var patchVersion: Int { return NSProcessInfo.processInfo().operatingSystemVersion.patchVersion }
var myOSVersion: String { return NSProcessInfo.processInfo().operatingSystemVersionString }
其他回答
根据Matt Thompson的回答,这里有一个带有各自单元测试的方法,可以在iOS 7及以上版本(包括iOS 9,不再让你检查NSFoundationNumber)上与Swift和Objective-c一起工作:
+ (BOOL) isAtLeastOSVersion:(NSString *)osVersion
{
switch ([[UIDevice currentDevice].systemVersion compare:osVersion options:NSNumericSearch]) {
case NSOrderedSame:
case NSOrderedDescending:
return YES;
default:
return NO;
}
}
.
@interface ANFakeCurrDevice : NSObject
@property (nonatomic, strong) NSString *systemVersion;
@end
@implementation ANFakeCurrDevice
@end
@implementation MyHelperClassUnitTests
- (void)setUp {
[super setUp];
}
- (void)tearDown {
[super tearDown];
}
- (void)test_isAtLeastOSVersion
{
id deviceMock = [OCMockObject niceMockForClass:[UIDevice class]];
ANFakeCurrDevice *fakeCurrDevice = [ANFakeCurrDevice new];
fakeCurrDevice.systemVersion = @"99.9.9";
[[[deviceMock stub] andReturn:fakeCurrDevice] currentDevice];
XCTAssertTrue([[UIDevice currentDevice].systemVersion isEqualToString:@"99.9.9"]);
fakeCurrDevice.systemVersion = @"1.0.1";
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"1"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"1.0"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"1.0.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"1.0.2"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"1.1.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.0.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.0.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.1.0"]);
fakeCurrDevice.systemVersion = @"8.4.0";
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"7.0.1"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8.4"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"8.4.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"8.4.2"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"9.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"9.0.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"9.0.2"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8.4"] && ![ANConstants isAtLeastOSVersion:@"9.0"]);
fakeCurrDevice.systemVersion = @"8.4.1";
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8.4"] && ![ANConstants isAtLeastOSVersion:@"9.0"]);
}
@end
如果你想查看WatchOS。
斯威夫特
let watchOSVersion = WKInterfaceDevice.currentDevice().systemVersion
print("WatchOS version: \(watchOSVersion)")
objective - c
NSString *watchOSVersion = [[WKInterfaceDevice currentDevice] systemVersion];
NSLog(@"WatchOS version: %@", watchOSVersion);
斯威夫特5
我们不需要创建扩展,因为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")
}
参考:http://nshipster.com/swift-system-version-checking/
对于iOS,请尝试:
var systemVersion = UIDevice.current.systemVersion
对于OS X,尝试:
var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion
如果你只是想检查用户是否至少运行了一个特定的版本,你也可以使用以下Swift 2功能,它适用于iOS和OS X:
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
}
快4.倍
func iOS_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) == ComparisonResult.orderedSame
}
func iOS_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) == ComparisonResult.orderedDescending
}
func iOS_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) != ComparisonResult.orderedAscending
}
func iOS_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) == ComparisonResult.orderedAscending
}
func iOS_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) != ComparisonResult.orderedDescending
}
用法:
if iOS_VERSION_GREATER_THAN_OR_EQUAL_TO(version: "11.0") {
//Do something!
}
附:KVISH回答翻译成Swift 4。x和重命名函数,因为我专门为iOS应用程序使用这个片段。