Joh*_*ohn 837 objective-c ios
我想检查iOS设备的版本是否大于3.1.3
我尝试过的东西:
[[UIDevice currentDevice].systemVersion floatValue]
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我只想要一个:
if (version > 3.1.3) { }
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
yas*_*urk 1052
/*
* System Versioning Preprocessor Macros
*/
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
/*
* Usage
*/
if (SYSTEM_VERSION_LESS_THAN(@"4.0")) {
...
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) {
...
}
Run Code Online (Sandbox Code Playgroud)
Jus*_*tin 979
由于雨燕2.0的,你可以使用#available在一个if或guard保护应该只在某些系统上运行的代码.
if #available(iOS 9, *) {}
在Objective-C中,您需要检查系统版本并执行比较.
[[NSProcessInfo processInfo] operatingSystemVersion] 在iOS 8及更高版本中.
从Xcode 9开始:
if (@available(iOS 9, *)) {}
在Objective-C和Swift中,在极少数情况下,最好避免依赖操作系统版本作为设备或操作系统功能的指示.通常有一种更可靠的方法来检查特定要素或类是否可用.
检查是否存在API:
例如,您可以使用以下方法检查UIPopoverController当前设备上是否可用NSClassFromString:
if (NSClassFromString(@"UIPopoverController")) {
// Do something
}
Run Code Online (Sandbox Code Playgroud)
对于弱链接类,直接向类消息是安全的.值得注意的是,这适用于未明确链接为"必需"的框架.对于缺少的类,表达式的计算结果为nil,条件不正确:
if ([LAContext class]) {
// Do something
}
Run Code Online (Sandbox Code Playgroud)
某些类(如CLLocationManager和UIDevice)提供了检查设备功能的方法:
if ([CLLocationManager headingAvailable]) {
// Do something
}
Run Code Online (Sandbox Code Playgroud)
检查是否存在符号:
偶尔,您必须检查是否存在常数.这在iOS 8中引入UIApplicationOpenSettingsURLString,用于加载设置应用程序-openURL:.在iOS 8之前该值不存在.将nil传递给此API将崩溃,因此您必须首先检查常量是否存在:
if (&UIApplicationOpenSettingsURLString != NULL) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
Run Code Online (Sandbox Code Playgroud)
与操作系统版本相比:
让我们假设您面临检查操作系统版本的相对罕见的需求.对于面向iOS 8及更高NSProcessInfo版本的项目,包括一种执行版本比较的方法,错误机会较少:
- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
Run Code Online (Sandbox Code Playgroud)
针对旧系统的项目可以使用systemVersionon UIDevice.Apple在其GLSprite示例代码中使用它.
// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
displayLinkSupported = TRUE;
}
Run Code Online (Sandbox Code Playgroud)
如果由于某种原因您决定这systemVersion是您想要的,请确保将其视为字符串,否则您可能会截断修补程序修订号(例如3.1.2 - > 3.1).
Car*_*rlJ 252
正如建议苹果官方文档:您可以使用NSFoundationVersionNumber,从NSObjCRuntime.h头文件.
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
// here you go with iOS 7
}
Run Code Online (Sandbox Code Playgroud)
Cœu*_*œur 100
在Objective-C中启动Xcode 9 :
if (@available(iOS 11, *)) {
// iOS 11 (or newer) ObjC code
} else {
// iOS 10 or older code
}
Run Code Online (Sandbox Code Playgroud)
在Swift中启动Xcode 7 :
if #available(iOS 11, *) {
// iOS 11 (or newer) Swift code
} else {
// iOS 10 or older code
}
Run Code Online (Sandbox Code Playgroud)
对于版本,您可以指定MAJOR,MINOR或PATCH(有关定义,请参阅http://semver.org/).例子:
iOS 11并且iOS 11.0是相同的最小版本iOS 10,iOS 10.3,iOS 10.3.1是不同的最小版本您可以为任何这些系统输入值:
iOS,macOS,watchOS,tvOS从我的一个pod获取的真实案例:
if #available(iOS 10.0, tvOS 10.0, *) {
// iOS 10+ and tvOS 10+ Swift code
} else {
// iOS 9 and tvOS 9 older code
}
Run Code Online (Sandbox Code Playgroud)
Tra*_* M. 44
这用于在Xcode中检查兼容的SDK版本,如果您有一个拥有不同Xcode版本的大型团队或支持共享相同代码的不同SDK的多个项目:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
//programming in iOS 8+ SDK here
#else
//programming in lower than iOS 8 here
#endif
Run Code Online (Sandbox Code Playgroud)
你真正想要的是检查设备上的iOS版本.你可以这样做:
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
//older than iOS 8 code here
} else {
//iOS 8 specific code here
}
Run Code Online (Sandbox Code Playgroud)
Swift版本:
if let version = Float(UIDevice.current.systemVersion), version < 9.3 {
//add lower than 9.3 code here
} else {
//add 9.3 and above code here
}
Run Code Online (Sandbox Code Playgroud)
当前版本的swift应该使用这个:
if #available(iOS 12, *) {
//iOS 12 specific code here
} else {
//older than iOS 12 code here
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*pan 37
尝试:
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
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*sko 35
在Swift 2.0中,Apple使用更方便的语法添加了可用性检查(在此处阅读更多内容).现在,您可以使用更清晰的语法检查操作系统版本:
if #available(iOS 9, *) {
// Then we are on iOS 9
} else {
// iOS 8 or earlier
}
Run Code Online (Sandbox Code Playgroud)
这是首选的检查respondsToSelector等(Swift中的新功能).现在,如果您没有正确地保护代码,编译器将始终警告您.
iOS 8中的新功能NSProcessInfo允许更好的语义版本检查.
对于iOS 8.0或更高版本的最低部署目标,请使用
NSProcessInfooperatingSystemVersion或isOperatingSystemAtLeastVersion.
这将产生以下结果:
let minimumVersion = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 1, patchVersion: 2)
if NSProcessInfo().isOperatingSystemAtLeastVersion(minimumVersion) {
//current version is >= (8.1.2)
} else {
//current version is < (8.1.2)
}
Run Code Online (Sandbox Code Playgroud)
对于iOS 7.1或更低版本的最低部署目标,请使用与
NSStringCompareOptions.NumericSearchon 进行比较UIDevice systemVersion.
这会产生:
let minimumVersionString = "3.1.3"
let versionComparison = UIDevice.currentDevice().systemVersion.compare(minimumVersionString, options: .NumericSearch)
switch versionComparison {
case .OrderedSame, .OrderedDescending:
//current version is >= (3.1.3)
break
case .OrderedAscending:
//current version is < (3.1.3)
fallthrough
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
更多阅读NSHipster.
我总是将它们保存在我的Constants.h文件中:
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
#define IS_OS_5_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
#define IS_OS_6_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define IS_OS_7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
Run Code Online (Sandbox Code Playgroud)
+(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement{
// eg NSString *reqSysVer = @"4.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:minRequirement options:NSNumericSearch] != NSOrderedAscending)
{
return YES;
}else{
return NO;
}
}
Run Code Online (Sandbox Code Playgroud)
随着版本包含在类NV-IOS版本项目(Apache许可证2.0版),很容易得到和比较的iOS版本.下面的示例代码转储iOS版本并检查版本是否大于或等于6.0.
// Get the system version of iOS at runtime.
NSString *versionString = [[UIDevice currentDevice] systemVersion];
// Convert the version string to a Version instance.
Version *version = [Version versionWithString:versionString];
// Dump the major, minor and micro version numbers.
NSLog(@"version = [%d, %d, %d]",
version.major, version.minor, version.micro);
// Check whether the version is greater than or equal to 6.0.
if ([version isGreaterThanOrEqualToMajor:6 minor:0])
{
// The iOS version is greater than or equal to 6.0.
}
// Another way to check whether iOS version is
// greater than or equal to 6.0.
if (6 <= version.major)
{
// The iOS version is greater than or equal to 6.0.
}
Run Code Online (Sandbox Code Playgroud)
项目页面: nv-ios-version
TakahikoKawasaki/nv-ios-version
博客:在运行时
获取和比较iOS版本与Version类在运行时获取并比较iOS版本与Version类
我知道这是一个老问题,但有人应该提到Availability.h. 这里的所有其他方法都是运行时解决方案,并且不适用于头文件、类类别或 ivar 定义。
对于这些情况,请使用
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0 && defined(__IPHONE_14_0)
// iOS 14+ code here
#else
// Pre iOS 14 code here
#endif
Run Code Online (Sandbox Code Playgroud)
h/t这个答案
使用swift Forget [[UIDevice currentDevice] systemVersion]和NSFoundationVersionNumber检查系统版本的新方法.
我们可以使用NSProcessInfo -isOperatingSystemAtLeastVersion
import Foundation
let yosemite = NSOperatingSystemVersion(majorVersion: 10, minorVersion: 10, patchVersion: 0)
NSProcessInfo().isOperatingSystemAtLeastVersion(yosemite) // false
Run Code Online (Sandbox Code Playgroud)
的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
Run Code Online (Sandbox Code Playgroud)
的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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
442620 次 |
| 最近记录: |