国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發之版本檢測

2019-11-06 09:53:19
字體:
來源:轉載
供稿:網友

由于各種原因,程序需要兼容iOS以前的版本,那么設置一個較低的部署目標和基于特定iOS版本的代碼分支,就顯得很有必要了。 舉個例子: 以前我們判斷iOS版本會如下寫:

if ([[[UIDevice currentDevice].systemVersion substringToIndex:1] intValue] >= 7) { // iOS 7.0 及以后的版本 } else { // iOS 7.0 以前的版本 }

但在iOS 10.0以后,如果這樣寫就會有問題,因為iOS 10.0會被檢測成 iOS 1.0了。所以說這個方法已經行不通了。

if ([[UIDevice currentDevice].systemVersion compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) { // iOS 7.0 及以后的版本 } else { // iOS 7.0 以前的版本 }

或者:

if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0) { // iOS 7.0 及以后的版本} else { // iOS 7.0 以前的版本}或者:if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0) { // iOS 7.0 及以后的版本} else { // iOS 7.0 以前的版本}

當然在iOS8.0中,Apple也提供了NSPRocessInfo 這個類來檢測版本問題。

@property (readonly) NSOperatingSystemVersion operatingSystemVersion NS_AVAILABLE(10_10, 8_0);- (BOOL) isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version NS_AVAILABLE(10_10, 8_0);

所以可以這樣檢測:

if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){.majorVersion = 8, .minorVersion = 3, .patchVersion = 0}]) { // iOS 8.3 及以后的版本} else { // iOS 8.3 以前的版本}

以為考慮到NSProcessInfo 是在iOS8.0才添加進來的,所以還是有它的局限性。

下面,我以UIAlertViewController 來說明。

NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController

NS_CLASS_AVAILABLE_IOS(8_0) 這個宏說明,UIAlertViewController 是在iOS8.0才被引進來的API,那如果我們在iOS7.0上使用,應用程序就會掛掉,那么如何在iOS8.0及以后的版本使用UIAlertViewController ,而在iOS8.0以前的版本中仍然使用UIAlertView 呢?

這里我們會介紹一下在#import <AvailabilityInternal.h> 中的兩個宏定義:

__ipHONE_OS_VERSION_MIN_REQUIRED

__IPHONE_OS_VERSION_MAX_ALLOWED

從字面意思就可以直到,__IPHONE_OS_VERSION_MIN_REQUIRED 表示iPhone支持最低的版本系統,__IPHONE_OS_VERSION_MAX_ALLOWED 表示iPhone允許最高的系統版本。

__IPHONE_OS_VERSION_MAX_ALLOWED 的取值來自iOS SDK的版本,比如我現在使用的是Xcode Version 8.2.1(8C1002),SDK版本是iOS 10.2,怎么看Xcode里SDK的iOS版本呢?

進入PROJECT,選擇Build Setting,在Architectures中的Base SDK中可以查看當前的iOS SDK版本。

打印這個宏,可以看到它一直輸出100200。

__IPHONE_OS_VERSION_MIN_REQUIRED 的取值來自項目TARGETS的Deployment Target,即APP愿意支持的最低版本。如果我們修改它為8.2,打印這個宏,會發現輸出80200,默認為10.2。

通常,__IPHONE_OS_VERSION_MAX_ALLOWED 可以代表當前的SDK的版本,用來判斷當前版本是否開始支持或具有某些功能。而__IPHONE_OS_VERSION_MIN_REQUIRED 則是當前SDK支持的最低版本,用來判斷當前版本是否仍然支持或具有某些功能。

回到UIAlertViewController 使用的問題,我們就可以使用這些宏,添加版本檢測判斷,從而使我們的代碼更健壯。

- (void)showAlertView {#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [alertView show];#else if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_8_0) { UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; [alertViewController addAction:cancelAction]; [alertViewController addAction:otherAction]; [self presentViewController:alertViewController animated:YES completion:NULL]; }#endif}

參考鏈接:

http://www.360doc.com/content/14/0521/10/11029609_379561686.shtmlhttp://www.tuicool.com/articles/3InIjavhttps://segmentfault.com/a/1190000006174131http://www.tuicool.com/articles/NrmAzmv
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉安市| 肃北| 寿阳县| 奉化市| 定远县| 博罗县| 揭阳市| 武夷山市| 萝北县| 昌吉市| 贞丰县| 怀来县| 郯城县| 荥阳市| 内黄县| 文水县| 彭水| 福泉市| 龙南县| 内丘县| 增城市| 伊金霍洛旗| 镇沅| 滦南县| 八宿县| 扎鲁特旗| 沙坪坝区| 隆化县| 三都| 乡城县| 江口县| 宣城市| 东海县| 台山市| 香河县| 正阳县| 广南县| 尼勒克县| 惠东县| 宁化县| 福安市|