LoginSignup
5

More than 5 years have passed since last update.

iOSでAndroidのapplicationIdSuffixと同じことを実現する

Posted at

Androidで開発/本番環境の切り替えを行う場合は、Build Variantをそれぞれ用意して制御します。

また、Build Variant毎にapplicationIdSuffixを設定することで、環境毎に別のアプリとして端末にインストールできます。

iOSでも同様の機能を実現したので紹介します。

はじめに、もっと簡単な方法の紹介

  • 本記事を書いた後に見つけたのですが、これから紹介するようなScriptを書かなくても実現できる方法がありました。

http://llcc.hatenablog.com/entry/2015/07/29/010020

  • 本記事の方法はgitのブランチ名をsuffixにつけるなどより複雑な要件の時には使えると思い、紹介させて頂きます。

前提

実行環境

  • macOS Sierra 10.12.4
  • Xcode 8.3.2
  • Swift 3.1
  • Deployment Target 9.0

Scheme

  • Development
  • Staging
  • Production

Configuration

Scheme毎にDebugとReleaseを用意しています。

  • Debug-Development
  • Debug-Staging
  • Debug-Production
  • Release-Development
  • Release-Staging
  • Release-Production

実現方法

  • Build PhasesRun Scriptに以下のScriptを追加します。
  • なお、ScriptにはAppIDの変更だけではなく、アプリ名の変更も含まれています。
plutil="/usr/bin/plutil"
plistBuddy="/usr/libexec/PlistBuddy"

infoPlistFileSource="${SRCROOT}/${INFOPLIST_FILE}"
infoPlistFileDestination="${TEMP_DIR}/Preprocessed-Info.plist"

appName=$($plistBuddy -c "Print CFBundleDisplayName" $infoPlistFileSource)
appNamePrefix=""

appId=$($plistBuddy -c "Print CFBundleIdentifier" $infoPlistFileSource)
appIdSuffix=""

if [[ ${CONFIGURATION} == *"Development"* ]]; then
appNamePrefix="DEV"
appIdSuffix=".dev"
fi

if [[ ${CONFIGURATION} == *"Staging"* ]]; then
appNamePrefix="STG"
appIdSuffix=".stg"
fi

newAppName=$appNamePrefix$appName
$plutil -replace 'CFBundleDisplayName' -string $newAppName $infoPlistFileDestination

newAppId=$appId$appIdSuffix
$plutil -replace 'CFBundleIdentifier' -string $newAppId $infoPlistFileDestination

補足

fastlaneでエラー

  • この設定を行い、デバッグは出来ていたのですが、fastlaneのgym(export_method: "ad-hoc")No matching provisioning profiles found for 〜というエラーが出てしまいました。
  • この点に関して、しっかりと検証できていないのですが、おそらく、変更後のAppIDと、そのAppIDのProvisioning Profileが登録済みでなかったからだと思います。
  • 一度、XcodeでArchive、Exportでipaを作ると、XcodeがAppIDとsonoAppIDのProvisioning Profileを登録します。
  • その後はfastlaneで先程のエラーは発生しなくなりました。

APNS Provisioning Profile

  • Push通知を使っている方はsuffix付きのAPNS Provisioning Profileを使用する必要があります。

最後に

AppIDやProvisioning Profileなど、一部理解が曖昧なところがありますが、ご紹介させて頂きました
参考になれば嬉しく思います。

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
5