iOS:多環(huán)境配置

1.多target配置配合多Scheme

1.1.創(chuàng)建多個target

image.png

點擊Duplicate,創(chuàng)建target,

1.2.設(shè)置預(yù)處理宏

我們可以在如下圖設(shè)置控制環(huán)境的參數(shù)


image.png

我們在TestNext的Build Setting中的Preprocess Macros中添加NEXT_EN=1
在我們原本的target中的Build Setting中的Preprocess Macros中添加NEXT_EN=0

1.3設(shè)置多個Scheme

我們可以新建Scheme,設(shè)置target為我們創(chuàng)建的第二個target

1.4.根據(jù)不同的target編譯不同的文件

我們可以在Build Phases中的Compile Sources中根據(jù)不同的targe設(shè)置不同的m文件


image.png

1.5真正的使用

我們創(chuàng)建兩個OC類,分別是FirstEnClass和NextEnClass
我們讓FirstEnClass在原本環(huán)境使用,NextEnClass在第二環(huán)境使用


image.png

image.png

我們可以在代碼去做如下操作

#if NEXT_EN
#import "NextEnClass.h"
#else
#import "FirstEnClass.h"
#endif

#if NEXT_EN
    NextEnClass *nextObject = [NextEnClass new];  
#else
    FirstEnClass *object = [FirstEnClass new];
#endif

我們也可以給兩個target分別設(shè)置Bundle identifieder,也可以設(shè)置App Icons Source等等信息。

2.多Scheme實現(xiàn)多環(huán)境

如果我們想感覺debug和release環(huán)境太單一,想再擁有bata環(huán)境,怎么辦呢?我們可以進行如下操作

2.1創(chuàng)建beta環(huán)境

選擇Project-》info-》Configurations中的+標(biāo)志。


image.png

2.2根據(jù)不同環(huán)境設(shè)置scheme

如果我們想切換環(huán)境,我們就必須去edit schems,這樣操作起來比較麻煩,我們可以創(chuàng)建多個scheme,每個scheme設(shè)置不一樣的環(huán)境。


image.png

每個scheme設(shè)置不同的環(huán)境。

2.3不同環(huán)境設(shè)置不同的參數(shù)

我們知道測試環(huán)境和生產(chǎn)環(huán)境的host不相同,我們可以在User-Defined中設(shè)置


image.png

2.4使用我們配置不同的參數(shù)

我們可以在plist文件中創(chuàng)建HOST_URL值,value就是我們設(shè)置成${HOST_URL}


image.png
NSString *path = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSLog(@"---%@",dictionary[@"HOST_URL"]);

2.5多環(huán)境的其他配置

1.AppIcons配置,在target中的build setting 的Asset Catalog中
2.Bundle identifier 在target中的build setting 的Product Bundle Identifier
3.還有很多,在此不在復(fù)述。

3.config文件配置多環(huán)境

我們在項目中使用cocoapods,

3.1創(chuàng)建config文件

res.gif

3.2.設(shè)置config文件

image.png

3.3引用cocoapods中的xcconfig

#include "Pods/Target Support Files/Pods-TestManyEnvironment/Pods-TestManyEnvironment.debug.xcconfig"

3.4.設(shè)置多環(huán)境參數(shù)

//在debug的config下
A=/
HOST_URL=https:${A}/www.debug.com
//在release的config下
A=/
HOST_URL=https:${A}/www.release.com

image.png

我們好奇為什么會定義A呢,是因為如果我們直接寫成HOST_URL=https://www.debug.com,編譯器會默認//是注釋符號。
使用和前面的一樣在plist文件中添加key值進行訪問。

3.5系統(tǒng)配置參數(shù)

我們可以在 Xcode Build Settings看配置參數(shù)。
我們用OTHER_LDFLAGS做說明

OTHER_LDFLAGS= -framework "UIKit"

我們可以在build setting看到


image.png

我們可以使用

OTHER_LDFLAGS=$(inherited)  -framework "UIKit"

把include文件中定義的OTHER_LDFLAGS繼承過來。
OTHER_LDFLAGS指定

OTHER_LDFLAGS[config=Debug][sdk=iphonesimulator*[arch=x86_64]= $(inherited) -framework "Cat"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容