Xcodeのxcconfigファイルを使った環境切り替えのサンプルプロジェクトです。
📝 解説記事: iOSアプリでxcconfigを利用して環境出し分けを実装する
Config/
├── Common.xcconfig # 共通設定
├── DebugBase.xcconfig # Debug共通設定
├── ReleaseBase.xcconfig # Release共通設定
├── ApiLocal.xcconfig # Local環境API
├── ApiStg.xcconfig # Staging環境API
├── ApiProd.xcconfig # Production環境API
├── DebugLocal.xcconfig # Debug + Local
├── DebugStg.xcconfig # Debug + Staging
├── DebugProd.xcconfig # Debug + Production
├── ReleaseLocal.xcconfig # Release + Local
├── ReleaseStg.xcconfig # Release + Staging
└── ReleaseProd.xcconfig # Release + Production
| Build Configuration | xcconfig |
|---|---|
| Debug | DebugStg.xcconfig |
| Release | ReleaseProd.xcconfig |
-xcconfigオプションを使うことで、Build Configurationを変更せずにxcconfigを切り替えられます。
# Debug ConfigurationでProduction環境を使用
xcodebuild -scheme xcconfigSample -configuration Debug \
-xcconfig xcconfigSample/Config/DebugProd.xcconfig \
-destination 'generic/platform=iOS Simulator' \
build# Debug ConfigurationでLocal環境を使用
xcodebuild -scheme xcconfigSample -configuration Debug \
-xcconfig xcconfigSample/Config/DebugLocal.xcconfig \
-destination 'generic/platform=iOS Simulator' \
build<key>ApiHost</key>
<string>$(ApiHost)</string>enum Const {
static let apiHost: String = {
guard let host = Bundle.main.object(forInfoDictionaryKey: "ApiHost") as? String else {
fatalError("ApiHost is not set in Info.plist")
}
return host
}()
}| 変数名 | 説明 | 例 |
|---|---|---|
ApiHost |
API接続先ホスト | local.host, stg.host, prod.host |
CommonString |
共通文字列 | Common String |
DisplayName |
アプリ表示名 | Debug App, Release App |
BundleIdentifier |
Bundle ID | org.satorun.sample.debug, org.satorun.sample |
AppIconName |
アプリアイコン名 | AppIconDebug, AppIcon |