2014-03-13 113 views
7

我使用此代码检测的iOS 7.1版本

#define IS_IOS7 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) 

要了解,如果应用程序是在iOS7运行。 但我现在需要知道,如果它运行在iOS7.1,但没有任何NSFoundationVersionNumber_iOS_7_0NSFoundationVersionNumber_iOS_7_1

我知道

#define NSFoundationVersionNumber_iOS_6_1 993.00 

的定义,也许我可以用一些比较高于993但我不知道。 有人获得安全可靠的解决方案?

+3

看起来像7.1是1047.25 –

回答

9

有几种方法可以做到这一点,你可以很容易地在SO上的几个答案中找到它们。这里有一些:

[[UIDevice currentDevice] systemVersion] 

稍微复杂一些,带测试:

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { 
    // iOS7... 
} 

您可以添加到测试的版本是这样一个更完整的方法:

/* 
* 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")) { 
    ... 
} 

来源:

How to check iOS version?

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

+0

它是好的,但它比NSFoundationVerstionNumber更慢。 – Dzmitry

+0

@Dzmitry什么比较慢?我提出了3种方法,其中NSFoundationVersionNumber是第二种... – Moonwalkr

+0

3号版本比2号版本慢。我检查了它。 – Dzmitry

1

定义的修改Marco

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) 

你可以这样

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.1")){ 
    //do something 
} 
2

检查版本试试下面的代码

float fOSVersion=[[[UIDevice currentDevice] systemVersion] floatValue]; 
    if(fOSVersion>7.0)//if it is greater than 7.0 
    { 
      //do your stuff here   
    }