LoginSignup
5
2

More than 5 years have passed since last update.

Configurationによってinfo.plistを書き換えてATSの有効/無効を切り替える

Posted at

背景と実現したい内容

背景として次のような開発環境になっています。

  • 開発環境のサーバがATS要件(TLS 1.1)を満たしていない
  • 担当者ごとに開発サーバが異なりWebAPI/WebViewのURLが異なる

このような環境で開発を行っており、担当者ごとのURLをATSの除外リストとしてinfo.plistに記載することが現実的ではなかったため、開発アプリでのみATSを無効にする事にしました。

前提

実行環境

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

Configuration

本記事は以下のConfigurationがあるアプリを前提に記載します。

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

実現方法

info.plist

info.plistNSAppTransportSecurityを追加していない状態にしておきます。

Preprocess Info.plist file

Build Settings > Pakaging > Preprocess Info.plist fileYES にします。

Run Script

  • Build PhasesRun Scriptを追加します。
  • Scriptは以下です。注意点等はコメントに書いています。
# plutilでdictの中の値を更新する方法が不明だったので、remove and insertで対応しました。
plutil="/usr/bin/plutil"
infoPlistFileDestination="${TEMP_DIR}/Preprocessed-Info.plist"

# 前回の状態が残るようで一度 remove する必要があります。
$plutil -remove 'NSAppTransportSecurity' $infoPlistFileDestination

if [[ ${CONFIGURATION} != *"Production"* ]]; then
$plutil -insert 'NSAppTransportSecurity' -xml '<dict><key>NSAllowsArbitraryLoads</key><true/></dict>' $infoPlistFileDestination
fi

参考にしたページ

以下のページを参考に実装させて頂きました。

さいごに

今回はこの方法で実現できました。

ただ、後から見返すと、Preprocess Info.plist fileはConfiguration毎にYES/NOを設定できるのでPreprocessed-Info.plistの有るなしで制御しても良いかもです。

また、ScriptのところでNSAppTransportSecurityを置き換えてしまっているのが微妙です。
この方法だと、一部ATS無効なURLを設定する場合にこのScriptを更新する必要があります。

本当はNSAllowsArbitraryLoadsの値を変えたかったのですが、plutilを使ってdictの中の値を更新する方法がわからず今回の方法にしました

5
2
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
5
2