How can we programmatically detect which iOS version is device running on?

meetpd

I want to check if the user is running the app on iOS less than 5.0 and display a label in the app.

How do I detect which iOS is running on user's device programmatically?

Thanks!

Marek Sebera

Best current version, without need to deal with numeric search within NSString is to define macros (See original answer: Check iPhone iOS Version)

Those macros do exist in github, see: https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h

Like this:

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

and use them like this:

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

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

Outdated version below

to get OS version:

[[UIDevice currentDevice] systemVersion]

returns string, which can be turned into int/float via

-[NSString floatValue]
-[NSString intValue]

like this

Both values (floatValue, intValue) will be stripped due to its type, 5.0.1 will become 5.0 or 5 (float or int), for comparing precisely, you will have to separate it to array of INTs check accepted answer here: Check iPhone iOS Version

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

and compare like this

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

For Swift 4.0 syntax

below example is just checking if the device is of iOS11 or greater version.

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can we detect notification cancel?

分類Dev

How to detect programmatically if "Android App" is running in chrome book or in Android phone

分類Dev

Qt: How to detect which version of OpenGL is being used?

分類Dev

How can we programmatically approve merge requests in GitLab?

分類Dev

How can a JavaScript app detect if a Leap Motion device is connected

分類Dev

How do you check which version of Meteor you are running?

分類Dev

This iPhone 6 is running iOS 12.4.1 (16G102), which may not be supported by this version of Xcode

分類Dev

This iPhone 6 is running iOS 12.4.1 (16G102), which may not be supported by this version of Xcode

分類Dev

How can I tell which web server Zimbra 8.0.6 is running?

分類Dev

How can I tell which user limit I am running into?

分類Dev

Can one determine the iOS SDK version used to build a binary, programmatically, at run time?

分類Dev

How can I conditionally run a suite of tests based on iOS device?

分類Dev

how can I check which version of vim I have installed?

分類Dev

Latest build of iOS app not running on device

分類Dev

Which version of Unity am I running?

分類Dev

Which Ruby version am I really running?

分類Dev

Which version of Unity am I running?

分類Dev

How to detect if ios8 custom keyboard extension is running in not iphone 6 optimized app?

分類Dev

kotlin,How value of Actual variable we can change in another class to which it passed using parameter?

分類Dev

How can we make the height of second div same as the height of first div which is dynamic?

分類Dev

how in angularjs we can achieve the object update which is based on some other objects?

分類Dev

In Django, after a login how can I detect which auth backend authenticated the user?

分類Dev

Go - how can i detect if the users PC is running some prerequisite tools such as Google Chrome for example

分類Dev

How can I detect whether my code is running "inside" Sidekiq server or Puma?

分類Dev

How can I save multiple pages of the PDF version of an Acumatica report to a file programmatically?

分類Dev

Programmatically get current running version of dotnet core runtime

分類Dev

How to upload iOS 6 app which can run on iOS 7 with iOS legacy Look

分類Dev

iOS - How to set a UISwitch programmatically

分類Dev

How can I tell if my Cordova application is running on a simulator or real device?

Related 関連記事

  1. 1

    How can we detect notification cancel?

  2. 2

    How to detect programmatically if "Android App" is running in chrome book or in Android phone

  3. 3

    Qt: How to detect which version of OpenGL is being used?

  4. 4

    How can we programmatically approve merge requests in GitLab?

  5. 5

    How can a JavaScript app detect if a Leap Motion device is connected

  6. 6

    How do you check which version of Meteor you are running?

  7. 7

    This iPhone 6 is running iOS 12.4.1 (16G102), which may not be supported by this version of Xcode

  8. 8

    This iPhone 6 is running iOS 12.4.1 (16G102), which may not be supported by this version of Xcode

  9. 9

    How can I tell which web server Zimbra 8.0.6 is running?

  10. 10

    How can I tell which user limit I am running into?

  11. 11

    Can one determine the iOS SDK version used to build a binary, programmatically, at run time?

  12. 12

    How can I conditionally run a suite of tests based on iOS device?

  13. 13

    how can I check which version of vim I have installed?

  14. 14

    Latest build of iOS app not running on device

  15. 15

    Which version of Unity am I running?

  16. 16

    Which Ruby version am I really running?

  17. 17

    Which version of Unity am I running?

  18. 18

    How to detect if ios8 custom keyboard extension is running in not iphone 6 optimized app?

  19. 19

    kotlin,How value of Actual variable we can change in another class to which it passed using parameter?

  20. 20

    How can we make the height of second div same as the height of first div which is dynamic?

  21. 21

    how in angularjs we can achieve the object update which is based on some other objects?

  22. 22

    In Django, after a login how can I detect which auth backend authenticated the user?

  23. 23

    Go - how can i detect if the users PC is running some prerequisite tools such as Google Chrome for example

  24. 24

    How can I detect whether my code is running "inside" Sidekiq server or Puma?

  25. 25

    How can I save multiple pages of the PDF version of an Acumatica report to a file programmatically?

  26. 26

    Programmatically get current running version of dotnet core runtime

  27. 27

    How to upload iOS 6 app which can run on iOS 7 with iOS legacy Look

  28. 28

    iOS - How to set a UISwitch programmatically

  29. 29

    How can I tell if my Cordova application is running on a simulator or real device?

ホットタグ

アーカイブ