【问题标题】:How can we programmatically detect which iOS version is device running on? [duplicate]我们如何以编程方式检测设备在哪个 iOS 版本上运行? [复制]
【发布时间】:2011-12-12 12:31:54
【问题描述】:

我想检查用户是否在低于 5.0 的 iOS 上运行应用并在应用中显示标签。

如何以编程方式检测用户设备上运行的是哪个 iOS?

谢谢!

【问题讨论】:

标签: iphone objective-c ios cocoa-touch ios4


【解决方案1】:

当前最佳版本,无需处理 NSString 中的数字搜索就是定义macros(见原答案:Check iPhone iOS Version

这些宏确实存在于 github 中,请参阅:https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h

像这样:

#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)

并像这样使用它们:

if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
    // code here
}

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
    // code here
}

以下是过时版本

获取操作系统版本:

[[UIDevice currentDevice] systemVersion]

返回字符串,可以通过转成int/float

-[NSString floatValue]
-[NSString intValue]

喜欢这个

两个值 (floatValue, intValue) 将因其类型而被剥离,5.0.1 将变为 5.0 或 5 (float 或 int),为了精确比较,您必须将其分隔为 INT 数组 在这里检查接受的答案:Check iPhone iOS Version

NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
float ver_float = [ver floatValue];

然后像这样比较

NSLog(@"System Version is %@",[[UIDevice currentDevice] systemVersion]);
NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if (ver_float < 5.0) return false;

适用于 Swift 4.0 语法

以下示例只是检查设备是否为iOS11 或更高版本。

let systemVersion = UIDevice.current.systemVersion
if systemVersion.cgFloatValue >= 11.0 {
    //"for ios 11"
  }
else{
   //"ios below 11")
  }

【讨论】:

  • 小心,因为 5.0.1 的浮点值是 5
  • @Michael 感谢您的通知,我已经添加了相关答案的链接
  • @SpencerWilliams 因为它不能很好地处理次要版本,如果您需要从 6 中识别 5 而不是从 5.1.0 识别 5.0.1 就足够了
  • 谢谢。我也有机会试用它,我可以确认该解决方案适用于 iOS 7。
  • 当心 - 这是正确的,但相对较慢。我刚刚找到了一些基于这个答案的代码作为 Instruments 中最重的跟踪的底部,它是从 scrollViewDidScroll 调用的: - 显然可以用不同的方式编写,但事实并非如此。
【解决方案2】:

更新

从 iOS 8 开始,我们可以在 NSProcessInfo 上使用新的 isOperatingSystemAtLeastVersion 方法

   NSOperatingSystemVersion ios8_0_1 = (NSOperatingSystemVersion){8, 0, 1};
   if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_1]) {
      // iOS 8.0.1 and above logic
   } else {
      // iOS 8.0.0 and below logic
   }

请注意,这将在 iOS 7 上崩溃,因为该 API 在 iOS 8 之前不存在。如果您支持 iOS 7 及更低版本,则可以安全地执行检查

if ([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
  // conditionally check for any version >= iOS 8 using 'isOperatingSystemAtLeastVersion'
} else {
  // we're on iOS 7 or below
}

原答案iOS

为了完整起见,这是 Apple 自己在 iOS 7 UI Transition Guide 中提出的另一种方法,其中涉及检查 Foundation Framework 版本。

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
   // Load resources for iOS 6.1 or earlier
} else {
   // Load resources for iOS 7 or later
}

【讨论】:

  • 这是目前最好的答案,应该在顶部。
  • 有点困扰我,可能会有 6.2 版本,然后大量代码会中断。但是,是的,Apple 推荐...
  • 请注意,根据此评论,6.1 和 6.0 的常量具有相同的值:stackoverflow.com/questions/19990900/…
  • 显然 Apple 尚未包含 7 或 7.1 的 NSFoundationVersionNumbers。所以我想不要用这种方法。
  • 请注意,调用isOperatingSystemAtLeastVersion 将触发一个无法识别的选择器发送到iOS 7 及更低版本的实例异常。
【解决方案3】:

我知道我回答这个问题已经太迟了。我不确定我的方法是否仍然适用于低 iOS 版本(

NSString *platform = [UIDevice currentDevice].model;

NSLog(@"[UIDevice currentDevice].model: %@",platform);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);

你可以得到这些结果:

[UIDevice currentDevice].model: iPhone
[UIDevice currentDevice].description: <UIDevice: 0x1cd75c70>
[UIDevice currentDevice].localizedModel: iPhone
[UIDevice currentDevice].name: Someones-iPhone002
[UIDevice currentDevice].systemVersion: 6.1.3
[UIDevice currentDevice].systemName: iPhone OS

【讨论】:

    【解决方案4】:
    [[UIDevice currentDevice] systemVersion]
    

    【讨论】:

      【解决方案5】:

      [[UIDevice currentDevice] systemVersion];

      或检查版本

      您可以从here 获取以下宏。

      if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_VERSION_3_2_0))      
      {
      
              UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cs_lines_back.png"]] autorelease];
              theTableView.backgroundView = background;
      
      }
      

      希望对你有帮助

      【讨论】:

      【解决方案6】:
      [[[UIDevice currentDevice] systemVersion] floatValue]
      

      【讨论】:

      • 最简单的方法。在 Swift (UIDevice.currentDevice().systemVersion as NSString).floatValue
      • 它也适用于我的情况。使用非常简单。
      • //大于10.0f---- if([[[UIDevice currentDevice] systemVersion] floatValue] > 10.0f){ }
      • 不适用于 6.0.7 等操作系统(其 floatValue 将为 7)版本字符串中的双小数点可能会破坏版本字符串上的“floatValue”。
      【解决方案7】:

      Marek Sebera 的大部分时间都很棒,但如果你像我一样发现需要经常检查 iOS 版本,你不想在内存中不断运行宏,因为你会体验到非常速度略有放缓,尤其是在旧设备上。

      相反,您希望将 iOS 版本计算为浮点数并将其存储在某处。就我而言,我有一个 GlobalVariables 单例类,我用它来检查我的代码中的 iOS 版本,使用如下代码:

      if ([GlobalVariables sharedVariables].iOSVersion >= 6.0f) {
          // do something if iOS is 6.0 or greater
      }
      

      要在您的应用中启用此功能,请使用以下代码(对于使用 ARC 的 iOS 5+):

      GlobalVariables.h

      @interface GlobalVariables : NSObject
      
      @property (nonatomic) CGFloat iOSVersion;
      
          + (GlobalVariables *)sharedVariables;
      
      @end
      

      GlobalVariables.m

      @implementation GlobalVariables
      
      @synthesize iOSVersion;
      
      + (GlobalVariables *)sharedVariables {
          // set up the global variables as a static object
          static GlobalVariables *globalVariables = nil;
          // check if global variables exist
          if (globalVariables == nil) {
              // if no, create the global variables class
              globalVariables = [[GlobalVariables alloc] init];
              // get system version
              NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
              // separate system version by periods
              NSArray *systemVersionComponents = [systemVersion componentsSeparatedByString:@"."];
              // set ios version
              globalVariables.iOSVersion = [[NSString stringWithFormat:@"%01d.%02d%02d", \
                                             systemVersionComponents.count < 1 ? 0 : \
                                             [[systemVersionComponents objectAtIndex:0] integerValue], \
                                             systemVersionComponents.count < 2 ? 0 : \
                                             [[systemVersionComponents objectAtIndex:1] integerValue], \
                                             systemVersionComponents.count < 3 ? 0 : \
                                             [[systemVersionComponents objectAtIndex:2] integerValue] \
                                             ] floatValue];
          }
          // return singleton instance
          return globalVariables;
      }
      
      @end
      

      现在您无需经常运行宏即可轻松检查 iOS 版本。请特别注意我是如何将[[UIDevice currentDevice] systemVersion] NSString 转换为CGFloat 的,该CGFloat 可以在不使用许多人已经在此页面上指出的任何不当方法的情况下不断访问。我的方法假定版本字符串的格式为 n.nn.nn(允许丢失后面的位)并且适用于 iOS5+。在测试中,这种方法比不断运行宏要快得多。

      希望这对遇到我遇到的问题的人有所帮助!

      【讨论】:

        【解决方案8】:

        在 MonoTouch 中:

        要获得主要版本,请使用:

        UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
        

        对于次要版本使用:

        UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
        

        【讨论】:

          【解决方案9】:

          要获得更具体的版本号信息,主要和次要版本分开:

          NSString* versionString = [UIDevice currentDevice].systemVersion;
          NSArray* vN = [versionString componentsSeparatedByString:@"."];
          

          数组vN 将包含主要和次要版本作为字符串,但如果您想进行比较,版本号应该存储为数字(整数)。您可以添加此代码以将它们存储在 C 数组中* versionNumbers:

          int versionNumbers[vN.count];
          for (int i = 0; i < sizeof(versionNumbers)/sizeof(versionNumbers[0]); i++)
              versionNumbers[i] = [[vN objectAtIndex:i] integerValue];
          

          * 此处使用 C 数组以获得更简洁的语法。

          【讨论】:

            【解决方案10】:

            低于 5 的 iOS 版本(所有版本)的简单检查:

            if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
                    // do something
            };
            

            【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-07-17
            • 1970-01-01
            • 2013-06-28
            • 2014-06-06
            • 2017-11-23
            相关资源
            最近更新 更多