LoginSignup
40
28

More than 5 years have passed since last update.

scheme毎にGoogleService-Info.plistを切り替える

Posted at

とあるiOSアプリのプロジェクトで、下記のような環境を用意しました。

  • サーバ側では、開発・ステージング・本番の3環境が用意されている。
    • 開発者は"開発"環境のサーバを利用する。
    • テスターに配布する際には、"ステージング"のサーバを利用する。
    • 本番申請時は、"本番"のサーバを利用する。
  • schemeを3つ定義しておき、それにより向き先を切り替えられるようにする。(CIなどでそれぞれのipaを作れるように)
  • Google Analyticsのtracking idは2つ用意されており、本番申請用とそれ以外で分ける。

tracking idの指定には、GoogleService-Info.plistを利用するのがイマドキのようです。
https://developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift&hl=ja

ただ、これをschemeで切り替えるような仕組みは標準では存在していないようです。
Androidであれば、build type毎に google-services.json 切り替えることが、標準で可能だったりします。 https://developers.google.com/android/guides/google-services-plugin#adding_the_json_file

そのため、Run Scriptを利用する形で無理やり実現しました。

前提

Scheme 用途 Configuration
app_dev 開発用 Debug
app_stg ステージング用 Staging
app_prd 本番用 Release

上記のようなscheme、Configurationになっていることを前提とします。

それぞれのファイルを配置

適当なディレクトリに、2つのplistを配置します。

Configuration/GoogleService-Info-debug.plist
...
    <key>TRACKING_ID</key>
    <string>UA-FOO-1</string>
...
Configuration/GoogleService-Info-release.plist
...
    <key>TRACKING_ID</key>
    <string>UA-BAR-2</string>
...

これらは、Xcode上でAdd Filesしておく必要がありません。(しても、ipaファイルに含まれてしまうだけですが)

Run Scriptに追加する

対象のtargetに下記のようなスクリプトを追加します。

if [ "${CONFIGURATION}" == "Release" ]; then
cp "${PROJECT_DIR}/${PROJECT_NAME}/Configuration/GoogleService-Info-release.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"
echo "Production GoogleService-Info copied."
else
cp "${PROJECT_DIR}/${PROJECT_NAME}/Configuration/GoogleService-Info-debug.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"
echo "Development GoogleService-Info copied."
fi

ここでは、生成するapp内にGoogleService-Info.plistを生成するのですが、
"Release" configurationの場合はGoogleService-Info-release.plistを、
それ以外のconfigurationの場合はGoogleService-Info-debug.plistをもとにします。

それ以外は、 https://developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift&hl=ja に書いてあるように実装していけば、よいかと。

40
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
28