如何为我的Xcode项目在我的podfile中指定多个目标?

我在我的Xcode 4项目中使用CocoaPods,我有三个目标(我的项目是默认的,一个用于构build精简版,一个用于构build演示版本)。 所有的目标使用相同的库,但CocoaPods只是添加静态库和searchpath的主要目标。 我的podfile看起来像这样:

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' 

我得到这个工作的唯一方法是单独指定每个目标,再次列出所有的豆荚。

 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 

有一个更好的方法吗?

CocoaPods 1.0目前处于testing阶段,并已经改变了语法。 现在看起来像这样:

 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 

过时的 CocoaPods 1.0回答:

是的,还有更好的办法! 看看link_with你可以做link_with 'MyApp', 'MyOtherApp'来指定多个目标。

我用link_with 'App', 'App-Tests' (小心目标名字中的空格)进行unit testing。

例:

 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' 

2017更新

你可以使用abstract_target

 # Note: There are no targets called "Shows" in any of this workspace's Xcode projects abstract_target 'Shows' do pod 'ShowsKit' # The target ShowsiOS has its own copy of ShowsKit (inherited) + ShowWebAuth (added here) target 'ShowsiOS' do pod 'ShowWebAuth' end # The target ShowsTV has its own copy of ShowsKit (inherited) + ShowTVAuth (added here) target 'ShowsTV' do pod 'ShowTVAuth' end # Our tests target has its own copy of # our testing frameworks, and has access # to ShowsKit as well because it is # a child of the abstract target 'Shows' target 'ShowsTests' do inherit! :search_paths pod 'Specta' pod 'Expecta' end end 

我认为更好的解决scheme是

 # 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 

参考: http : //natashatherobot.com/cocoapods-installing-same-pod-multiple-targets/

如果您希望多个目标共享相同的窗格,请使用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 

要不就

 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 

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

最简单的方法是使用一个抽象的目标,其中每个指定的pod将与所有目标链接。

 abstract_target 'someNameForAbstractTarget' do pod 'podThatIsForAllTargets' end target 'realTarget' do pod 'podThatIsOnlyForThisTarget' end