多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > 綜合技術 > Xcode使用xcconfig文件配置環(huán)境

Xcode使用xcconfig文件配置環(huán)境

來源:程序員人生   發(fā)布時間:2016-06-23 08:05:56 閱讀次數(shù):5005次

歡迎到個人博客: liumh.com查看本文。

與公司 QA 聊天,已不止1次被吐槽說移動端從開發(fā)環(huán)境轉到生產(chǎn)環(huán)境時,還要靠修改代碼來配置對應的環(huán)境參數(shù)。她認為,從 App 轉測試以后,就不應當再修改代碼,可以把所有的環(huán)境配置都整合到配置文件中,這樣打不同環(huán)境下的安裝包時,會自動選擇對應的環(huán)境參數(shù)。這里說到的環(huán)境參數(shù)包括但不但限于: webservice 地址,友盟 AppKey,極光推送 AppKey 和是不是是生產(chǎn)環(huán)境標志等。

其實,我也討厭修改環(huán)境參數(shù)啊~

為達成上述目的,主要是使用 Xcode 的 Configurations Setting File(即后綴為 xcconfig 文件) 來配置開發(fā)不同階段下的環(huán)境。本文包括的內(nèi)容以下:

  1. Xcode Target
  2. Xcode Project
  3. Build Setting的繼承關系
  4. 如何使用xcconfig文件來配置不同開發(fā)階段的環(huán)境

包括了1些與 build settings 相干的知識。

Xcode Target

target, 官方文檔以下解釋:

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 定義了生成的唯1 product, 它將構建該 product 所需的文件和處理這些文件所需的指令集整合進 build system 中。Projects 會包括1個或多個 targets,每個 target 將會產(chǎn)出1個 product.

The instructions for building a product take the form of build settings and build phases, which you can examine and edit in the Xcode project editor. A target inherits the project build settings, but you can override any of the project settings by specifying different settings at the target level. There can be only one active target at a time; the Xcode scheme specifies the active target.

這些指令以 build setting 和 build phases 的情勢存在,你可在 Xcode 的項目編輯器(TARGETS->Build Setting, TARGETS->Build Phases)中進行查看和編輯。target 中的 build setting 參數(shù)繼承自 project 的 build settings, 但是你可以在 target 中修改任意 settings 來重寫 project settings,這樣,終究生效的 settings 參數(shù)以在 target 中設置的為準. Project 可包括多個 target, 但是在同1時刻,只會有1個 target 生效,可用 Xcode 的 scheme 來指定是哪個 target 生效.

A target and the product it creates can be related to another target. If a target requires the output of another target in order to build, the first target is said to depend upon the second. If both targets are in the same workspace, Xcode can discover the dependency, in which case it builds the products in the required order. Such a relationship is referred to as an implicit dependency. You can also specify explicit target dependencies in your build settings, and you can specify that two targets that Xcode might expect to have an implicit dependency are actually not dependent. For example, you might build both a library and an application that links against that library in the same workspace. Xcode can discover this relationship and automatically build the library first. However, if you actually want to link against a version of the library other than the one built in the workspace, you can create an explicit dependency in your build settings, which overrides this implicit dependency.

target 和其生成的 product 可與另外一個 target 有關,如果1個 target 的 build 依賴于另外一個 target 的輸出,那末我們就說前1個 target 依賴于后1個 target .如果這些 target 在同1個 workspace 中,那末 Xcode 能夠發(fā)現(xiàn)這類依賴關系,從而使其以我們期望的順序生成 products.這類關系被稱為隱式依賴關系。同時,你可以顯示指定 targets 之間的依賴關系,并且這類依賴關系會覆蓋 Xcode 推測出的隱式依賴關系。

指定 targets 之間的依賴關系的地方在 Project Editor->TRAGETS->Build Phases->Target Dependencies 處設置。以下圖所示:

添加targets間的依賴關系

Xcode Project

官方文檔的解釋以下:

An Xcode project is a repository for all the files, resources, and information required to build one or more software products. A project contains all the elements used to build your products and maintains the relationships between those elements. It contains one or more targets, which specify how to build products. A project defines default build settings for all the targets in the project (each target can also specify its own build settings, which override the project build settings).

Xcode project 是1個倉庫,該倉庫包括了所有的文件,資源和用于生成1個或多個 software products 的信息。它包括1個或多個 targets,其中的每個 target 指明了如何生成 products。project 為其具有的所有 targets 定義了默許的 build settings,固然,每個 target 能夠制定其自己的 build settings,且 target 的 build settings 會重寫 project 的 build settings。

Xcode project 文件包括以下信息:

  • 源文件的援用:
    • 源碼,包括頭文件和實現(xiàn)文件
    • 內(nèi)部和外部的庫或框架
    • 資源文件
    • 圖片文件
    • Interface Builder(nib)文件
  • 文件結構導航中用來組織源文件的組
  • Project-level build configurations.你可以為 project 指定多個 build configuration,例如,project 中默許包括 debug 和 release 兩種 build settings.
  • Targets, 每個 target 指定了:
    • project 生成的 product
    • 生成 product 所需的源文件
    • 生成 product 所需的配置文件,包括對其他 targets 的依賴和1些其他設置;當 targets 的 build configurations 沒有重寫 project-level 的 build settings 時,會直接使用 project-level 的 build setting.
  • 可履行環(huán)境,該環(huán)境用于調(diào)試或測試程序,每一個可履行環(huán)境會指定:
    • 運行或調(diào)試程序時加載的可履行程序
    • 傳遞給可履行程序的命令行參數(shù)
    • 運行程序時需設置的環(huán)境變量

project 可獨立存在,也可被包括在 workspace 中。

Build Setting 的繼承關系

官方文檔內(nèi)容以下:

A build setting is a variable that contains information about how a particular aspect of a product’s build process should be performed. For example, the information in a build setting can specify which options Xcode passes to the compiler.

You can specify build settings at the project or target level. Each project-level build setting applies to all targets in the project unless explicitly overridden by the build settings for a specific target.

build setting 中包括了 product 生成進程中所需的參數(shù)信息。你可以在 project-level 和 target-level 層指定 build settings。project-level 的 build settings 適用于 project 中的所有targets,但是當 target-level 的 build settings 重寫了 project-level 的 build settings,以 target-level 中的 build settings 中的值為準。

Each target organizes the source files needed to build one product. A build configuration specifies a set of build settings used to build a target’s product in a particular way. For example, it is common to have separate build configurations for debug and release builds of a product.

1個 build configaration 指定了1套 build settings 用于生成某1 target 的 product,例如,在 Xcode 創(chuàng)建項目時默許就有兩套獨立的 build configarations, 分別用于生成 debug 和 release 模式下的 product。

In addition to the default build settings provided by Xcode when you create a new project from a project template, you can create user-defined build settings for your project or for a particular target. You can also specify conditional build settings. The value of a conditional build setting depends on whether one or more prerequisites are met. This mechanism allows you to, for example, specify the SDK to use to build a product based on the targeted architecture.

除創(chuàng)建工程時生成的默許 build settings,你也能夠自定義 project-level 或 target-level 的 build settings.

關于繼承關系,The Unofficial Guide to xcconfig files 這里也有詳細的說明,強烈建議瀏覽。

現(xiàn)在就來看看如何使用自定義的 build settings 來到達本文開始處提到的需求.

如何使用 xcconfig 文件來配置不同開發(fā)階段的環(huán)境

目前公司中的開發(fā)大致分兩個階段,第1階段:開發(fā)階段,此時所打包都是使用 development 的證書,極光和友盟統(tǒng)計的賬號都是使用開發(fā)者自己申請的賬號,webservice 的地址使用開發(fā)環(huán)地步址;第2階段:uat 階段,此時屬于預發(fā)版階段,此時打包使用 ad-hoc 的證書,極光和友盟統(tǒng)計的賬號使用公司申請生成賬號,webservice 使用的特定的預發(fā)版環(huán)境;另外,打上傳到 App Store 的生產(chǎn)包,使用 distribution 的證書,webservice 的地址使用生產(chǎn)環(huán)境的地址。

由此,可新建1種 build configuration, 由 Xcode 自動生成的 Release 復制而來,以下所示:

新建Configurations

并命名為 PreRelease。

官方文檔Adding a Build Configuration 中以下提到:

A configuration file is a plain text file with a list of build setting definitions, one per line. You can base a build configuration only on a configuration file that is in your project, not on an external file.

When you base a target or project’s build configuration on a configuration file, that build configuration automatically inherits the build setting definitions in that configuration file (and any configuration files it includes). If you then modify the value of any of those build settings in the target or project, the new value is used instead of the value in the configuration file.

Build settings defined at the target level override any values assigned to those build settings at the project level. Therefore, target-level configurations take precedence over any project-level configurations.

這里需要注意的是:當你的 target-level 或 project-levle 的 build configurations 基于配置文件時,build configuration 會自動繼承配置文件(和配置文件中引入的配置文件)中定義的 build settings,但是如果你又在以后 target 或 project 中修改了配置文件中定義的 build settings 值,那末終究配置文件中的值會失效,實際使用的是 target 或 project 中設置的值。

這里鑒于公司的情況,新建了 Debug.xcconfig/PreRelease.xcconfig/Release.xcconfig 配置對應于開發(fā)階段、預發(fā)版階段、上傳 AppStore 3種情況下的打包。

新建1個 xcconfig 目錄,在該目錄下新建配置文件:

創(chuàng)建配置文件

根據(jù)項目情況,每一個配置文件中都包括一樣的 key 值,內(nèi)容大致以下:

//網(wǎng)絡要求baseurl WEBSERVICE_URL = @"http:\/\/127.0.0.1" //友盟配置 UMENG_APPKEY = @"xxxvvv555999==" //極光推送配置 JPUSH_DEVELOPMENT_APPKEY = @"nnncccvvvwww" IS_PRODUCATION = NO #include "Generator.xcconfig"

你可在配置文件中包括其他配置文件,其中 Generator.xcconfig 文件的內(nèi)容是:

GCC_PREPROCESSOR_DEFINITIONS = $(inherited) WEBSERVICE_URL='$(WEBSERVICE_URL)' MESSAGE_SYSTEM_URL='$(MESSAGE_SYSTEM_URL)' UMENG_APPKEY='$(UMENG_APPKEY)' IS_PRODUCATION='$(IS_PRODUCATION)'

其作用是將配置文件中定義的常量定義成預編譯宏,以便于在代碼中獲得。

其中 GCC_PREPROCESSOR_DEFINITIONS, 文檔以下:

Space-separated list of option specifications. Specifies preprocessor macros in the form foo (for a simple #define) or foo=1 (for a value definition). This list is passed to the compiler through the gcc -D option when compiling precompiled headers and implementation files.

GCC_PREPROCESSOR_DEFINITIONS 是 GCC 預編譯頭參數(shù),通常我們可以在 Project 文件下的 Build Settings 對預編譯宏定義進行默許賦值。在 Xcode7 下的路徑為 Build Settings->Apple LLVM 7.x Preprocessing->Preprocessor Macros,

GCC_PREPROCESSOR_DEFINITIONS

想必大家看這個宏的名字已知道它的作用了, 使用上和在 pch 頭文件中添加宏定義沒有太大的區(qū)分, 但有以下好處:

  1. Xcode 的 Project 的 Build Settings 是由1個 plist 文件進行描寫的, plist 本質(zhì)上是1個 XML 配置文件, 通過外部的腳本比較容易去修改。
  2. Preprocessor Macros 可以依照 Configuration 選項進行默許配置, 也就是說可以根據(jù)不同的環(huán)境預先制定不同定義的宏,或為不同環(huán)境下的相同變量定義不同的值

xcconfig 支持可以根據(jù)不同的 Configuration 選項配置不同的文件。不同的 xcconfig 可以指定不同的 Build Settings 里的屬性值, 這模樣我們就能夠通過項目 xcconfig 去修改 GCC_PREPROCESSOR_DEFINITIONS 的值了(終究目的就到達了)。

配置文件中變量定義好以后,怎樣讓 Xcode 自動加載呢?以下圖設置所示,是將 project-level 的 build settings 基于配置文件,3種情況的 configurations 分別選擇與之對應的配置文件。

將配置文件與項目關聯(lián)

當我們想把 project-level 或 target-level 中的 Build Settings 的設置移動到 xcconfig 配置文件來設置時,是不是需要1個個手動輸入呢?固然不是,直接在 Build Settings 當選中你想要在 xcconfig 中配置的鍵值對所在行(固然也能夠選多行),command + c復制,然后到對應的 xcconfig 中去粘貼就行了,記得在 Build Settings 中改成你想要的值后再復制,如果為默許值的話則只可復制其鍵。如果需要改回去的話,還是選中這行,command + delete 就恢復默許值了。

現(xiàn)在我們將設置移動到了配置文件中,所有的配置文件都是鍵值對類型的文本文件,但是當同1個鍵同時存在于 target-level、project-level 和配置文件中時,究竟是哪個鍵值對起作用了呢?現(xiàn)在看看下圖。

多個配置項列

注意: Xcode以從左至右的順序設置解析的優(yōu)先級,從左至右優(yōu)先級下降,最左側的具有最高優(yōu)先級,即 target-level > project-level > 自定義配置文件 > iOS 默許配置;且最左列 Resolved 列顯示的是終究使用的值。那末如何使 Xcode 使用配置文件中的配置項呢?這需要選中要使用配置文件的行,點擊 Delete 按鍵,你會發(fā)現(xiàn)項目的默許設置已被刪除,且 xcconfig 的配置文件列被標記為綠色。標記為綠色代表該列的值生效,其值應當與 Resolved 列的值相同。

最后,你可以像以下示例使用 xcconfig 中定義的宏:

NSLog(@"webservice url: %@, umeng appkey: %@", WEBSERVICE_URL, UMENG_APPKEY);

通過以上步驟,就到達了使用 xcconfig 文件來配置開發(fā)不同階段時的環(huán)境變量的目的了。

文中內(nèi)容為自己學習總結,如有毛病的地方請指正。


如果覺得本文對你有幫助,就請用微信隨便打賞我吧^_^

請我喝杯咖啡^_^


參考:

  • Xcode Concepts
  • Using xcconfig files for your XCode Project
  • iOS開發(fā)必備 - 環(huán)境變量配置
  • xcconfig-how-to-set-environment-variables
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产做人爱三级视频在线 | 婷婷在线视频国产综合 | 亚洲精品蜜桃久久久久久 | 国产美女福利视频福利 | 亚洲成人免费在线观看 | 久久最新精品 | 日本一区二区免费在线观看 | 欧美日韩亚洲另类 | www一区二区| 亚洲视频在线观看免费 | 中文字幕一级 | 国产免费久久精品久久久 | 亚洲人人视频 | 久久不卡一区 | 在线观看男女激情小视频 | 婷婷在线成人免费观看搜索 | 国内自拍视频在线播放 | 一二三四视频在线观看免费高清 | 国产三级午夜理伦三级 | 日韩欧美国产中文 | 日韩精品手机在线 | 偷拍欧美视频 | 成年人在线视频免费观看 | 欧美videos另类极品 | 午夜欧美在线 | 国产第一页精品 | 中文字幕一区二区三区永久 | 国产精品福利在线观看秒播 | 欧式午夜理伦三级在线观看 | 亚洲天堂中文 | 国产性tv国产精品 | 第一页在线 | 欧美日韩乱国产 | 噜噜影院无毒不卡 | 亚洲an日韩专区在线 | 午夜色站 | 亚洲区中文字幕 | 国产亚洲欧美另类久久久 | 69av免费观看 | 欧美日韩一区二区综合 | 欧美一级毛级毛片 |