How do I specify multiple targets in my podfile for my Xcode project?

XcodeTargetCocoapods

Xcode Problem Overview


I'm using CocoaPods with my Xcode 4 project and I have three targets for my project (the default, one for building a lite version and one for building a demo version). All the targets use the same libraries, but CocoaPods is only adding the static library and search paths to the primary target. My podfile looks like this:

platform :ios, '5.0'

pod 'TestFlightSDK', '>= 1.1'
pod 'MBProgressHUD', '0.5'
pod 'iRate', '>= 1.6.2'
pod 'TimesSquare', '1.0.1'
pod 'AFNetworking', '1.1.0'
pod 'KKPasscodeLock', '0.1.5'
pod 'iCarousel', '1.7.4'

The only way I was get this to work was to specify each target individually with all the the pods listed again.

platform :ios, '5.0'

target :default do	
	pod 'TestFlightSDK', '>= 1.1'
	pod 'MBProgressHUD', '0.5'
	pod 'iRate', '>= 1.6.2'
	pod 'TimesSquare', '1.0.1'
	pod 'AFNetworking', '1.1.0'
	pod 'KKPasscodeLock', '0.1.5'
	pod 'iCarousel', '1.7.4'
end

target :lite do	
	link_with 'app-lite'
	
	pod 'TestFlightSDK', '>= 1.1'
	pod 'MBProgressHUD', '0.5'
	pod 'iRate', '>= 1.6.2'
	pod 'TimesSquare', '1.0.1'
	pod 'AFNetworking', '1.1.0'
	pod 'KKPasscodeLock', '0.1.5'
	pod 'iCarousel', '1.7.4'
end

target :demo do	
	link_with 'app-demo'
	
	pod 'TestFlightSDK', '>= 1.1'
	pod 'MBProgressHUD', '0.5'
	pod 'iRate', '>= 1.6.2'
	pod 'TimesSquare', '1.0.1'
	pod 'AFNetworking', '1.1.0'
	pod 'KKPasscodeLock', '0.1.5'
	pod 'iCarousel', '1.7.4'
end

Is there a better way to do this?

Xcode Solutions


Solution 1 - Xcode

Since CocoaPods 1.0 has changed the syntax, instead of using link_with, do something like:

# Note; name needs to be all lower-case.
def shared_pods
    pod 'SSKeychain', '~> 0.1.4'
    pod 'INAppStoreWindow', :head
    pod 'AFNetworking', '1.1.0'
    pod 'Reachability', '~> 3.1.0'
    pod 'KSADNTwitterFormatter', '~> 0.1.0'
    pod 'MASShortcut', '~> 1.1'
    pod 'MagicalRecord', '2.1'
    pod 'MASPreferences', '~> 1.0'
end

target 'Sail' do
    shared_pods
end

target 'Sail-iOS' do
    shared_pods
end

Old answer Pre CocoaPods 1.0:

Yes there is a better way! Check out link_with where you can do link_with 'MyApp', 'MyOtherApp' to specify multiple targets.

I use this with unit tests like link_with 'App', 'App-Tests' (beware of spaces in target's names).

Example:

platform :osx, '10.8'

link_with 'Sail', 'Sail-Tests'

pod 'SSKeychain', '~> 0.1.4'
pod 'INAppStoreWindow', :head
pod 'AFNetworking', '1.1.0'
pod 'Reachability', '~> 3.1.0'
pod 'KSADNTwitterFormatter', '~> 0.1.0'
pod 'MASShortcut', '~> 1.1'
pod 'MagicalRecord', '2.1'
pod 'MASPreferences', '~> 1.0'

Approach using abstract_target:

In below example, the 'ShowsiOS', 'ShowsTV' and 'ShowsTests' targets have their own separate pods, plus ShowsKit inherited, because they are all child of the dummy target 'Shows'.

# Note: There are no targets called "Shows" in any of this workspace's Xcode projects.
abstract_target 'Shows' do
  pod 'ShowsKit'

  target 'ShowsiOS' do
    pod 'ShowWebAuth'
  end

  target 'ShowsTV' do
    pod 'ShowTVAuth'
  end

  # Our tests target has its own copy
  # of our testing frameworks
  # (beside inheriting ShowsKit pod).

  target 'ShowsTests' do
    inherit! :search_paths
    pod 'Specta'
    pod 'Expecta'
  end
end

Solution 2 - Xcode

I think better solution is

# Podfile
 
platform :ios, '8.0'
 
use_frameworks!
 
# Available pods
 
def available_pods
    pod 'AFNetworking', '1.1.0'
    pod 'Reachability', '~> 3.1.0'
end
 
target 'demo' do
  available_pods
end
 
target 'demoTests' do
    available_pods
end

Reference from : http://natashatherobot.com/cocoapods-installing-same-pod-multiple-targets/

Solution 3 - Xcode

If you want multiple targets to share the same pods, use an abstract_target.

# There are no targets called "Shows" in any Xcode projects
abstract_target 'Shows' do
  pod 'ShowsKit'
  pod 'Fabric'

  # Has its own copy of ShowsKit + ShowWebAuth
  target 'ShowsiOS' do
    pod 'ShowWebAuth'
  end

  # Has its own copy of ShowsKit + ShowTVAuth
  target 'ShowsTV' do
    pod 'ShowTVAuth'
  end
end

or just

pod 'ShowsKit'
pod 'Fabric'

# Has its own copy of ShowsKit + ShowWebAuth
target 'ShowsiOS' do
  pod 'ShowWebAuth'
end

# Has its own copy of ShowsKit + ShowTVAuth
target 'ShowsTV' do
  pod 'ShowTVAuth'
end

source: https://guides.cocoapods.org/using/the-podfile.html

Solution 4 - Xcode

Easiest way is to use an abstract target, where each pod specified will be linked with all targets.

abstract_target 'someNameForAbstractTarget' do
  pod 'podThatIsForAllTargets'
end

target 'realTarget' do
  pod 'podThatIsOnlyForThisTarget'
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAustinView Question on Stackoverflow
Solution 1 - XcodeKeith SmileyView Answer on Stackoverflow
Solution 2 - XcodeAdarsh G JView Answer on Stackoverflow
Solution 3 - XcodeAdam SmakaView Answer on Stackoverflow
Solution 4 - XcodeShaked SayagView Answer on Stackoverflow