Help us understand the problem. What is going on with this article?

CocoaPodsでアプリ側ターゲットのbuild_settingsを変更する方法

More than 5 years have passed since last update.

Xcodeで、End-to-End testsやIntegration testsを実行するときに、テスト時だけ行う処理とか逆に行わない処理とかを書きたいと思いますが、
メソッド差し替え等の手段を使えない場合、下記のようにマクロを定義しておかないと余計な処理が走って面倒なことになります。

#ifndef APP_TESTING
    // テスト以外のときだけ実行する処理
    // 広告SDKとかが勝手にブラウザへ飛ばす処理とか
#endif

CocoaPodsを使っていると、アプリ側のbuild_settingsのPreprocessor Macrosが書き換わるので
同時にアプリで使用するマクロも、podコマンドを実行したときに一緒に定義する方法ないかなと思って、
試した所うまく行ったのでメモ

post_installのブロックでworkspaceかなんかを参照する変数かなんかあるのかとおもっていたのですが、
どうやら、ないようなのでxcodeprojを使ってアプリ側のprojectを開いて変更すればいいようです

Podfile
TESTING_TARGETS = %w[AppTests AppIntegrationTests]
post_install do |installer|
  # アプリ側のxcode projectを開く
  Xcodeproj::Project.open('App.xcodeproj').tap do |project|
    targets = project.targets.find_all do |target|
      TESTING_TARGETS.include?(target.name)
    end
    # Test用macro定義
    targets.each do |target|
      target.build_configurations.each do |config|
        if config.name == "Debug"
          preprocessor_definitions = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] || ['$(inherited)']
          preprocessor_definitions = [preprocessor_definitions] unless preprocessor_definitions.instance_of?(Array)
          preprocessor_definitions.push('APP_TESTING=1').uniq!

          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = preprocessor_definitions
        end
      end
    end

    project.save
  end
end
roothybrid7
I'm a programmer. Network Architect/iPhone developer/Unix/Linux/C/Objective-C/Java/Ruby/node.js/Python https://github.com/roothybrid7
https://github.com/roothybrid7
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした