我想检查设备的iOS版本是否大于3.1.3 我尝试了以下方法:

[[UIDevice currentDevice].systemVersion floatValue]

但是不管用,我只想要一个:

if (version > 3.1.3) { }

我怎样才能做到这一点呢?


当前回答

开始Xcode 9,在Objective-C中:

if (@available(iOS 11, *)) {
    // iOS 11 (or newer) ObjC code
} else {
    // iOS 10 or older code
}

开始Xcode 7,在Swift中:

if #available(iOS 11, *) {
    // iOS 11 (or newer) Swift code
} else {
    // iOS 10 or older code
}

对于版本,您可以指定MAJOR、MINOR或PATCH(参见http://semver.org/的定义)。例子:

iOS 11和iOS 11.0是相同的最小版本 iOS 10, iOS 10.3, iOS 10.3.1是不同的最小版本

您可以为这些系统中的任何一个输入值:

iOS, macOS, watchOS, tvOS

来自我的一个豆荚的真实案例:

if #available(iOS 10.0, tvOS 10.0, *) {
    // iOS 10+ and tvOS 10+ Swift code
} else {
    // iOS 9 and tvOS 9 older code
}

文档

其他回答

UIDevice + IOSVersion.h

@interface UIDevice (IOSVersion)

+ (BOOL)isCurrentIOSVersionEqualToVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionGreaterThanVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionGreaterThanOrEqualToVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionLessThanVersion:(NSString *)iOSVersion;
+ (BOOL)isCurrentIOSVersionLessThanOrEqualToVersion:(NSString *)iOSVersion

@end

UIDevice + IOSVersion.m

#import "UIDevice+IOSVersion.h"

@implementation UIDevice (IOSVersion)

+ (BOOL)isCurrentIOSVersionEqualToVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedSame;
}

+ (BOOL)isCurrentIOSVersionGreaterThanVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedDescending;
}

+ (BOOL)isCurrentIOSVersionGreaterThanOrEqualToVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] != NSOrderedAscending;
}

+ (BOOL)isCurrentIOSVersionLessThanVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] == NSOrderedAscending;
}

+ (BOOL)isCurrentIOSVersionLessThanOrEqualToVersion:(NSString *)iOSVersion
{
    return [[[UIDevice currentDevice] systemVersion] compare:iOSVersion options:NSNumericSearch] != NSOrderedDescending;
}

@end

Try:

NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"3.1.3" options: NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
    // OS version >= 3.1.3
} else {
    // OS version < 3.1.3
}

试试这个

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { 
// do some work
}

开始Xcode 9,在Objective-C中:

if (@available(iOS 11, *)) {
    // iOS 11 (or newer) ObjC code
} else {
    // iOS 10 or older code
}

开始Xcode 7,在Swift中:

if #available(iOS 11, *) {
    // iOS 11 (or newer) Swift code
} else {
    // iOS 10 or older code
}

对于版本,您可以指定MAJOR、MINOR或PATCH(参见http://semver.org/的定义)。例子:

iOS 11和iOS 11.0是相同的最小版本 iOS 10, iOS 10.3, iOS 10.3.1是不同的最小版本

您可以为这些系统中的任何一个输入值:

iOS, macOS, watchOS, tvOS

来自我的一个豆荚的真实案例:

if #available(iOS 10.0, tvOS 10.0, *) {
    // iOS 10+ and tvOS 10+ Swift code
} else {
    // iOS 9 and tvOS 9 older code
}

文档

仅用于检索OS版本字符串值:

[[UIDevice currentDevice] systemVersion]