2014-03-13 111 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   
    }