LoginSignup
6
7

More than 5 years have passed since last update.

[iOS] ターゲットごとに Asset Catalog( .xcassets) を切り替えたい場合

Last updated at Posted at 2016-01-04

※下記のTipsは CocoaPods を使用している前提です。

App Icon や Launch Imageは簡単にターゲット毎に反映できますが、
.xcassets 内の画像はうまく反映されないことがあります。

その際の対処方法について

開発環境

Xcode 7.2
Swift 2.0
CocoaPods 0.39.0

Tips

1 ) 分岐用に xcassets を複数作成

一例
image.xcassets //=> 本番用
image_dev.xcassets //=> dev用

※一部の画像だけを分岐させたい場合は共通画像用の「xcassets」も作成するなど
プロジェクト毎にカスタマイズを

2) Target Memebershipを分岐

image.xcassets
c734dedd54f0a3965affea1a077eaa05.png

image_dev.xcassets
82b75a5ee57dd7953e10107c65df17c5.png

3) Podfile内に下記を追記して、 pod install

Podfile
post_install do |installer|
    installer.pods_project.targets.each do |target|
      folder_path = "Pods/Target\\ Support\\ Files/Pods/Pods"
      %x~ if [ ! -f #{folder_path}-resources.sh.bak ]; then cp #{folder_path}-resources.sh #{folder_path}-resources.sh.bak; fi ~

      %x~ sed '/WRAPPER_EXTENSION/,/fi\\n/d' #{folder_path}-resources.sh > #{folder_path}-resources.sh.temp ~
      %x~ sed '/*.xcassets)/,/;;/d' #{folder_path}-resources.sh.temp > #{folder_path}-resources.sh ~
      %x~ rm #{folder_path}-resources.sh.temp ~
    end
end

以上です。これでターゲット毎に.xcassets 内の画像が反映されます。

Pod install時にやってること

Pods > Targets Support Files > Pods > Pods-resources.sh
下記の箇所が削除されます(コメントアウトしてるところ)
Screen_Shot_2015-12-04_at_2_31_53_PM.png

6
7
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
6
7