你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發之不同版本適配問題2(#ifdef __IPHONE_7_0)

IOS開發之不同版本適配問題2(#ifdef __IPHONE_7_0)

編輯:IOS開發綜合

繼續說說ios不同版本之間的適配

先說一個東西:在xcode當中有一個東西叫targets,蘋果的官方文檔是這樣說的:

A target specifies a product to build and contains the instructions for building the product from a set of files in a project or workspace. A target defines a single product; it organizes the inputs into the build system—the source files and instructions for processing those source files—required to build that product. Projects can contain one or more targets, each of which produces one product.(省略若干字)

簡單翻譯過來就是一個target詳細說明了要構建的產品,包含了一個工程或工作目錄中的文件當中的指令,貌似還是不太清楚。在谷歌搜了一下,發現這個關於target的解釋還是不錯的:

A target basically defines what it is you are building and how you are building it. You can add more targets if you would like to build more than one thing. This usually makes sense if you need to build several related things from the same project. For instance, you might want one target for a full, paid version of an application, and another target for a reduced, free version of an application. Both targets would include much of the same code and resources, but some of the settings would be different and you might have different files included with each.

意思就是如果你想將一個應用分為付費完整版和免費簡化版,這兩個版本大部分代碼是一樣的,只有個別的設置和包含的文件不同,你就可以建一個application然後弄兩個targets分別對應兩個版本。

下面舉個簡單的例子來說明在iOS7.0和iOS6.1(以及更低版本)之間的適配問題(用的是xcode5.0,裡邊有6.1和7.0兩個版本的sdk)

新建一個工程,默認的development target,base sdk以及模擬器的版本都是7.0,在AppDelegate中的didFinishLaunchingWithOptions方法裡寫下

 

        self.window.tintColor = [UIColor redColor];

然後運行,這樣是沒有任何錯誤的。接下來將development target,base sdk以及模擬器的版本都改成6.1(注意默認的xcode是沒有6.1的sdk的,需要自己另外導入)。然後運行,就會報錯:

 

\

也就是說tintColor屬性在iOS6.1中根本就沒有,在編譯時候就會出錯。這時候如下加上判斷語句也是沒有用的,照樣報錯

 

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.window.tintColor = [UIColor redColor];
    }

遇見這種情況只能加上預處理語句,這樣寫:

 

 

#ifdef __IPHONE_7_0
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.window.tintColor = [UIColor redColor];
    }
#endif

這樣編譯通過就不會報錯了……這是因為在sdk6.1下的usr/include下邊有一個Availability.h文件,裡邊定義了一大堆宏,其中關於iphone的有

 

 

#define __IPHONE_2_0     20000
#define __IPHONE_2_1     20100
#define __IPHONE_2_2     20200
#define __IPHONE_3_0     30000
#define __IPHONE_3_1     30100
#define __IPHONE_3_2     30200
#define __IPHONE_4_0     40000
#define __IPHONE_4_1     40100
#define __IPHONE_4_2     40200
#define __IPHONE_4_3     40300
#define __IPHONE_5_0     50000
#define __IPHONE_5_1     50100
#define __IPHONE_6_0     60000
#define __IPHONE_6_1     60100
#define __IPHONE_NA      99999  /* not available */

而sdk7.0裡邊多了一行

 

 

#define __IPHONE_7_0     60100

 

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved