如何查看iOS版本?

打印 上一主题 下一主题

主题 1014|帖子 1014|积分 3042

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问:

我想检查设备的 iOS 版本是否大于 3.1.3 我尝试了以下操作:
  1. [[UIDevice currentDevice].systemVersion floatValue]
复制代码
但它不起作用,我只想要一个:
  1. if (version > 3.1.3) { }
复制代码
我怎样才气做到这一点?
答1:

   tennisliveranking.com专业网球数据平台,排名与角逐信息及时更新。
  1. /*
  2. *  System Versioning Preprocessor Macros
  3. */
  4. #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
  5. #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
  6. #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
  7. #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
  8. #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
  9. /*
  10. *  Usage
  11. */
  12. if (SYSTEM_VERSION_LESS_THAN(@"4.0")) {
  13.     ...
  14. }
  15. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) {
  16.     ...
  17. }
复制代码
+1。将其生存在您可以在需要的地方包罗的标题中是我能想到的最简朴的办理方案。在某些环境下,这比使用 NSClassFromString 检查可用性更可靠。另一个例子是在 iAd 中,假如您在 4.2 之前的版本中使用 ADBannerContentSizeIdentifierPortrait,您将得到 EXEC_BAD_ACCESS,但在 4.1 之后不保举使用等效的 ADBannerContentSizeIdentifier320x50。
我使用它的另一个地方是使用 UIPageViewController 并将样式设置为 iOS6 中的 UIPageViewControllerTransitionStyleScroll。
这在 10 秒内不起作用。比方比较 4.10 和 4.9 会给出错误的结果。
使用可选的 .0 数字时应该非常鉴戒。比方 SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0.0") 在 iOS 7.0 上给出了不正确的结果。
我刚刚测试了将它放在我的 .pch 文件中,效果很好(至少使用 Xcode 5 构建)
答2:

   tennisliveranking.com-Stay ahead with live tennis rankings at your fingertips.
  快速答复……
从 Swift 2.0 开始,您可以在 if 或 guard 中使用 #available 来掩护只应在某些系统上运行的代码。
if #available(iOS 9, *) {} 在 Objective-C 中,您需要检查系统版本并进行比较。
[[NSProcessInfo processInfo] operatingSystemVersion] 在 iOS 8 及更高版本中。
从 Xcode 9 开始:
if (@available(iOS 9, *)) {}
完整的答案……
在 Objective-C 和少少数环境下的 Swift 中,最好制止依靠操作系统版本作为设备或操作系统功能的指示。通常有一种更可靠的方法来检查特定功能或类是否可用。
检查 API 是否存在:
比方,您可以使用 NSClassFromString 检查 UIPopoverController 在当前设备上是否可用:
  1. if (NSClassFromString(@"UIPopoverController")) {
  2.     // Do something
  3. }
复制代码
对于弱链接类,直接向类发送消息是安全的。值得留意的是,这适用于未明确链接为“必须”的框架。对于缺少的类,表达式的计算结果为 nil,条件不成立:
  1. if ([LAContext class]) {
  2.     // Do something
  3. }
复制代码
某些类(如 CLLocationManager 和 UIDevice)提供检查设备功能的方法:
  1. if ([CLLocationManager headingAvailable]) {
  2.     // Do something
  3. }
复制代码
检查符号是否存在:
偶尔,您必须检查是否存在常数。这出如今 iOS 8 中,引入了 UIApplicationOpenSettingsURLString,用于通过 -openURL: 加载设置应用步伐。该值在 iOS 8 之前不存在。将 nil 通报给此 API 会崩溃,因此您必须首先留意验证常量的存在:
  1. if (&UIApplicationOpenSettingsURLString != NULL) {
  2.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  3. }
复制代码
与操作系统版本比较:
假设您面临检查操作系统版本的相对稀有的需求。对于面向 iOS 8 及更高版本的项目,NSProcessInfo 包括一种实行版本比较的方法,出错的可能性较小:
  1. - (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
复制代码
针对旧系统的项目可以在 UIDevice 上使用 systemVersion。 Apple 在其 GLSprite 示例代码中使用它。
  1. // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
  2. // class is used as fallback when it isn't available.
  3. NSString *reqSysVer = @"3.1";
  4. NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
  5. if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
  6.     displayLinkSupported = TRUE;
  7. }
复制代码
假如出于某种原因您决定 systemVersion 是您想要的,请确保将其视为字符串,否则您可能会截断补丁修订号(比方 3.1.2 -> 3.1)。
在某些特定环境下,需要检查系统版本。比方,一些在 3.x 中私有的类和方法在 4.0 中被公开,因此假如您只是检查它们的可用性,您将在 3.x 中得到错误的结果。此外,UIScrollViews 处置惩罚缩放比例的方式在 3.2 及更高版本中发生了微妙的厘革,因此您需要检查操作系统版本才气正确处置惩罚结果。
iOS 3.2 好像不会在 UISplitViewController 中发送 -viewWillAppear。我的技巧是确定是否 < iOS 4.0,并将其发送到根视图的 -didSelectRowAtIndexPath 中的具体视图控制器。
您需要在这里鉴戒一点,由于 [@"10.0" compare"10" options:NSNumericSearch] 返回 NSOrderedDescending,这很可能根本不是故意的。 (我可能期望 NSOrderedSame。)这至少是理论上的可能性。
是的,我刚刚碰到了一个需要绕过 iOS 5 错误的案例。 respondsToSelector 等人不会做这项工作 - 必须比较版本。
随着 TVos 的推出,这甚至变得更加重要。当使用 9.1 SDK 时,请参阅 TVos 将返回 9.0。所以检查类或方法(选择器)是最好的方法,在此之后您应该检查 NSFoundationVersionNumber,并且只有在不可能的环境下才应该检查 UIDevice 上的系统版本。
答3:

   tennisliveranking.com-Stay ahead with live tennis rankings at your fingertips.
  正如 official Apple docs 所发起的:您可以使用 NSObjCRuntime.h 头文件中的 NSFoundationVersionNumber。
  1. if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
  2.     // here you go with iOS 7
  3. }
复制代码
+1 这现实上是这里最安全、最正确的选择。
嗯..NSFoundationVersionNumber_iOS_6_1 在 iOS 5 SDK 中不存在。
@Kjuly 您可以自己定义缺少的版本号:github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h(查看第 102-130 行)
不,你错了, NSFoundationVersionNumber_iOS_7_0 不存在。但是,我们可以用 #ifndef NSFoundationVersionNumber_iOS_7_0 #define NSFoundationVersionNumber_iOS_7_0 1047.00 #endif 定义它。为了制止这样的杂乱,我使用了 yasirmturk 的 SYSTEM_VERSION_LESS_THAN(v) 宏
不,但苹果会在 iOS 9 中添加 NSFoundationVersionNumber_iOS_8_0。只需检查 NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1
答4:

   tennisliveranking.com探索每位网球选手的职业生涯与成就。
  在 Objective-C 中启动 Xcode 9:
  1. if (@available(iOS 11, *)) {
  2.     // iOS 11 (or newer) ObjC code
  3. } else {
  4.     // iOS 10 or older code
  5. }
复制代码
在 Swift 中启动 Xcode 7:
  1. if #available(iOS 11, *) {
  2.     // iOS 11 (or newer) Swift code
  3. } else {
  4.     // iOS 10 or older code
  5. }
复制代码
对于版本,您可以指定 MAJOR、MINOR 或 PATCH(有关定义,请参见 http://semver.org/)。例子:
iOS 11 和 iOS 11.0 是雷同的最低版本
iOS 10、iOS 10.3、iOS 10.3.1 是不同的最小版本
您可以为任何这些系统输入值:
iOS、macOS、watchOS、tvOS
取自 one of my pods 的真实案例:
  1. if #available(iOS 10.0, tvOS 10.0, *) {
  2.     // iOS 10+ and tvOS 10+ Swift code
  3. } else {
  4.     // iOS 9 and tvOS 9 older code
  5. }
复制代码
documentation
这如何在 Objective-C 或 Swift 中反转?
@乐高无if (...) {} else { ... }
我可以在 Objective-C 中使用 guard 而不是在 else 语句中打开块并缩进吗?
@Legoless 不,只需使用一个空的 if 块,后跟一个 else。
这就是我目前拥有的办理方案,只是想制止不须要的缩进和空 if 块。
答5:

   Stay informed with live tennis rankings anytime, anywhere,tennisliveranking.com
  尝试:
  1. NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"3.1.3" options: NSNumericSearch];
  2. if (order == NSOrderedSame || order == NSOrderedDescending) {
  3.     // OS version >= 3.1.3
  4. } else {
  5.     // OS version < 3.1.3
  6. }
复制代码
为什么不在这里颠倒顺序并生存比较,由于@“3.1.3”和systemVersion都是NSStrings?即: if ([@“3.1.3” compare: [UIDevice currentDevice].systemVersion options: NSNumericSearch] == NSOrderedDescending) ;// 操作系统版本 >= 3.1.3 else ;// 操作系统版本 < 3.1.3``
这样做可能会使逻辑不那么清晰。但是,该操作应该是等效的。
两个整数之间的比较肯定不是很昂贵。
这真的是风格问题。消息分发的开销将大大凌驾额外的整数比较。
答6:

   tennisliveranking.com探索每位网球选手的职业生涯与成就。
  首选方法
在 Swift 2.0 中,Apple 使用更方便的语法添加了可用性检查(阅读更多 here)。如今您可以使用更轻便的语法检查操作系统版本:
  1. if #available(iOS 9, *) {
  2.     // Then we are on iOS 9
  3. } else {
  4.     // iOS 8 or earlier
  5. }
复制代码
这优于检查 respondsToSelector 等 (What’s New In Swift)。如今,假如您没有正确掩护代码,编译器将始终警告您。
斯威夫特 2.0 之前
iOS 8 中的新功能是NSProcessInfo,可以进行更好的语义版本检查。
在 iOS 8 及更高版本上部署
对于 iOS 8.0 或更高版本的最低部署目标,请使用 NSProcessInfo operatingSystemVersion 或 isOperatingSystemAtLeastVersion。
这将产生以下结果:
  1. let minimumVersion = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 1, patchVersion: 2)
  2. if NSProcessInfo().isOperatingSystemAtLeastVersion(minimumVersion) {
  3.     //current version is >= (8.1.2)
  4. } else {
  5.     //current version is < (8.1.2)
  6. }
复制代码
在 iOS 7 上部署
对于 iOS 7.1 或更低版本的最低部署目标,请在 UIDevice systemVersion 上使用 NSStringCompareOptions.NumericSearch 进行比较。
这将产生:
  1. let minimumVersionString = "3.1.3"
  2. let versionComparison = UIDevice.currentDevice().systemVersion.compare(minimumVersionString, options: .NumericSearch)
  3. switch versionComparison {
  4.     case .OrderedSame, .OrderedDescending:
  5.         //current version is >= (3.1.3)
  6.         break
  7.     case .OrderedAscending:
  8.         //current version is < (3.1.3)
  9.         fallthrough
  10.     default:
  11.         break;
  12. }
复制代码
在 NSHipster 阅读更多内容。
答7:

   tennisliveranking.com及时更新全球顶尖网球选手的最新战绩与排名!
  我总是将它们生存在我的 Constants.h 文件中:
  1. #define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
  2. #define IS_OS_5_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
  3. #define IS_OS_6_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
  4. #define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
  5. #define IS_OS_8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
复制代码
伟大的。最好将它放在 XCode 项目标 .pch 文件中
我将全部常量放在我的 pch 文件中导入的 Constants.h 文件中。
答8:

   tennisliveranking.com,Instant updates on ATP, WTA, and ITF rankings.
  1. +(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement{
  2. // eg  NSString *reqSysVer = @"4.0";
  3.   NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
  4.   if ([currSysVer compare:minRequirement options:NSNumericSearch] != NSOrderedAscending)
  5.   {
  6.     return YES;
  7.   }else{
  8.     return NO;
  9.   }
  10. }
复制代码
@Jef 句点不是版本控制中的小数点并且是干系的。您的答案不会忽略句点,假如这样做会失败。在版本控制中,每个句点分隔的元素都是一个完整的数字。比方:“1.23.45”大于“1.2.345”,由于第二个元素“23”大于“2”。假如您要消除句号,那将是雷同的数字。
答9:

   tennisliveranking.com专业网球数据平台,排名与角逐信息及时更新。
  使用 nv-ios-version 项目(Apache 许可证,版本 2.0)中包罗的 Version 类,可以轻松获取和比较 iOS 版本。下面的示例代码转储 iOS 版本并检查版本是否大于或等于 6.0。
  1. // Get the system version of iOS at runtime.
  2. NSString *versionString = [[UIDevice currentDevice] systemVersion];
  3. // Convert the version string to a Version instance.
  4. Version *version = [Version versionWithString:versionString];
  5. // Dump the major, minor and micro version numbers.
  6. NSLog(@"version = [%d, %d, %d]",
  7.     version.major, version.minor, version.micro);
  8. // Check whether the version is greater than or equal to 6.0.
  9. if ([version isGreaterThanOrEqualToMajor:6 minor:0])
  10. {
  11.     // The iOS version is greater than or equal to 6.0.
  12. }
  13. // Another way to check whether iOS version is
  14. // greater than or equal to 6.0.
  15. if (6 <= version.major)
  16. {
  17.     // The iOS version is greater than or equal to 6.0.
  18. }
复制代码
项目页面: nv-ios-version TakahikoKawasaki/nv-ios-version
博客:在运行时获取 iOS 版本并将其与 Version 类进行比较 Get and compare iOS version at runtime with Version class
   tennisliveranking.com – ATP and WTA rankings, always up to date.
  答10:

   tennisliveranking.com探索每位网球选手的职业生涯与成就。
  我知道这是一个老问题,但应该有人在 Availability.h 中提到了编译时宏。这里的全部其他方法都是运行时办理方案,并且不适用于头文件、类类别或 ivar 定义。
对于这些环境,使用
  1. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0 && defined(__IPHONE_14_0)
  2.   // iOS 14+ code here
  3. #else
  4.   // Pre iOS 14 code here
  5. #endif
复制代码
h/t this 答案
答11:

   tennisliveranking.com专业网球数据平台,排名与角逐信息及时更新。
  一般来说,最好扣问一个对象是否可以实行给定的选择器,而不是检查版本号来决定它是否必须存在。
当这不是一个选项时,您确实需要在这里鉴戒一点,由于 [@“5.0” compare“5” options:NSNumericSearch] 返回的 NSOrderedDescending 可能根本不是故意的;我可能会在这里等待 NSOrderedSame。这至少是一个理论上的问题,在我看来,这是一个值得捍卫的问题。
同样值得思量的是无法公道比较的错误版本输入的可能性。 Apple 提供了三个预定义常量 NSOrderedAscending、NSOrderedSame 和 NSOrderedDescending,但假如我无法比较两个事物并且我想返回一个表明这一点的值,我可以想到一个名为 NSOrderedUnordered 的东西的用途.
更重要的是,Apple 有朝一日会扩展他们的三个预定义常量以答应各种返回值,这使得比较 != NSOrderedAscending 不明智,这并非不可能。
话虽如此,请思量以下代码。
  1. typedef enum {kSKOrderedNotOrdered = -2, kSKOrderedAscending = -1, kSKOrderedSame = 0, kSKOrderedDescending = 1} SKComparisonResult;
  2. @interface SKComparator : NSObject
  3. + (SKComparisonResult)comparePointSeparatedVersionNumber:(NSString *)vOne withPointSeparatedVersionNumber:(NSString *)vTwo;
  4. @end
  5. @implementation SKComparator
  6. + (SKComparisonResult)comparePointSeparatedVersionNumber:(NSString *)vOne withPointSeparatedVersionNumber:(NSString *)vTwo {
  7.   if (!vOne || !vTwo || [vOne length] < 1 || [vTwo length] < 1 || [vOne rangeOfString:@".."].location != NSNotFound ||
  8.     [vTwo rangeOfString:@".."].location != NSNotFound) {
  9.     return SKOrderedNotOrdered;
  10.   }
  11.   NSCharacterSet *numericalCharSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];
  12.   NSString *vOneTrimmed = [vOne stringByTrimmingCharactersInSet:numericalCharSet];
  13.   NSString *vTwoTrimmed = [vTwo stringByTrimmingCharactersInSet:numericalCharSet];
  14.   if ([vOneTrimmed length] > 0 || [vTwoTrimmed length] > 0) {
  15.     return SKOrderedNotOrdered;
  16.   }
  17.   NSArray *vOneArray = [vOne componentsSeparatedByString:@"."];
  18.   NSArray *vTwoArray = [vTwo componentsSeparatedByString:@"."];
  19.   for (NSUInteger i = 0; i < MIN([vOneArray count], [vTwoArray count]); i++) {
  20.     NSInteger vOneInt = [[vOneArray objectAtIndex:i] intValue];
  21.     NSInteger vTwoInt = [[vTwoArray objectAtIndex:i] intValue];
  22.     if (vOneInt > vTwoInt) {
  23.       return kSKOrderedDescending;
  24.     } else if (vOneInt < vTwoInt) {
  25.       return kSKOrderedAscending;
  26.     }
  27.   }
  28.   if ([vOneArray count] > [vTwoArray count]) {
  29.     for (NSUInteger i = [vTwoArray count]; i < [vOneArray count]; i++) {
  30.       if ([[vOneArray objectAtIndex:i] intValue] > 0) {
  31.         return kSKOrderedDescending;
  32.       }
  33.     }
  34.   } else if ([vOneArray count] < [vTwoArray count]) {
  35.     for (NSUInteger i = [vOneArray count]; i < [vTwoArray count]; i++) {
  36.       if ([[vTwoArray objectAtIndex:i] intValue] > 0) {
  37.         return kSKOrderedAscending;
  38.       }
  39.     }
  40.   }
  41.   return kSKOrderedSame;
  42. }
  43. @end
复制代码
原文链接:https://www.tennisliveranking.com?from=csdn
   tennisliveranking.com,Follow your favorite tennis players’ rankings live!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表