7

I have a project with over 50 targets and growing and it is becoming cumbersome to add files to all targets because it takes a long time to select each target.

I am aware of multiple methods to add a file to multiple targets but they all involve checking a box for each target. (for those looking for that: How to add .plist file to all targets in XCode?)

What I'm looking for is an alternative method or script that can be used to add a file or set of files to all targets in the project without selecting them one by one. Anyone know of a trick?

Community
  • 1
  • 1
BFar
  • 2,447
  • 22
  • 23
  • You can probably manipulate the `project.pbxproj` file yourself (better quit Xcode while you're doing that). But there's probably a better way, like adding the files to single framework, and then integrate that framework in all the projects. – jcaron Dec 29 '16 at 01:32
  • Hmm good point, I suppose could make my project with many targets very basic and abstract most code into private pods. – BFar Dec 29 '16 at 02:04
  • You can use https://github.com/CocoaPods/Xcodeproj lib, same which is using cocoapods – hsafarya Mar 07 '18 at 20:38

2 Answers2

2

Here is a link to GitHub repo. In the example project I have three targets, added file only in first target. When you run ruby ./addfile.rb from project's directory, the script will add img.jpg resource into two other targets (projex2, projex3). You need to install xcodeproj ruby gem before running the script. You should run sudo gem install xcodeproj in terminal.

hsafarya
  • 1,043
  • 1
  • 10
  • 21
  • Facing error after using the script. /Users/user/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/xcodeproj-1.7.0/lib/xcodeproj/project/object/build_phase.rb:62:in `build_file': undefined method `referrers' for nil:NilClass (NoMethodError) from /Users/user/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/xcodeproj-1.7.0/lib/xcodeproj/project/object/build_phase.rb:72:in `include?' from /Users/user/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/xcodeproj-1.7.0/lib/xcodeproj/project/object/native_target.rb:521:in `block in add_resources' Please let me know if i have missed anything – user2071152 Oct 24 '18 at 04:52
  • Did you try to run example project ? If no, then can you ? – hsafarya Oct 24 '18 at 08:49
  • I have a workspace and different target but the sample has different project. The sample and my workspace are bit different structured. – user2071152 Oct 24 '18 at 12:42
  • I want to understand what got wrong, I can't help without additional info, or debugging. – hsafarya Oct 24 '18 at 17:54
0

What I'm looking for is an alternative method or script that can be used to add a file or set of files to all targets in the project without selecting them one by one. Anyone know of a trick?

If you're often adding the same source to all your targets, then one way to avoid that would be to create a framework the contains all the files your various targets have in common, and then have each of those targets link in the framework. That way, you only add common files to the one framework, and all the targets get access to them via the framework.

Caleb
  • 124,013
  • 19
  • 183
  • 272