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


142

我在Xcode 4项目中使用CocoaPods,并且我的项目有三个目标(默认情况下,一个目标用于构建lite版本,一个目标用于构建演示版本)。所有目标都使用相同的库,但是CocoaPods仅将静态库和搜索路径添加到主要目标。我的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'

使我工作的唯一方法是使用列出的所有Pod分别指定每个目标。

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

有一个更好的方法吗?


请阅读有关抽象目标的信息。这就是你所需要的。guides.cocoapods.org/syntax/podfile.html#abstract_target
— Nik Kov,

Answers:


339

CocoaPods 1.0更改了此语法。现在看起来像这样:

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'((注意目标名称中的空格)。

例:

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

太好了,那么您将link_with放在我的第一个示例podfile中呢?能给我举个例子吗?
— 奥斯丁

更新了我的答案。没关系。
— Keith Smiley

4
我正在尝试同一件事,但就我而言,我链接到主目标的多个目标依赖项。这导致在链接阶段出现重复的符号错误。您知道如何使用Cocoapods解决此问题吗?
— Fergal Rooney

2
看起来不再需要目标列表周围的括号(并且不起作用?)。设计师:guides.cocoapods.org/syntax/podfile.html#link_with
— toblerpwn 2014年

2
@KeithSmiley我明白了。实际上,我仍然在那些空间上遇到麻烦。我必须将所有目标重命名为没有空格。Cocoapods没有链接(对所有目标都这样做)而不是link_with。
— Hishamaus

91

我认为更好的解决方案是

# 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/


1
您介意解释为什么这是一个更好的解决方案吗?
— 2015年

1
@Warpling:请浏览此natashatherobot.com/…–
— Adarsh GJ

9
如果您在此处添加了一些说明,那将是很棒的。(最好保留所有必要的信息,以防链接link_with
— 断开

我喜欢这种方法,因为它允许为所有目标(available_pods)和目标特定的Pod提供一堆Pod。
— Apoc

该解决方案可以正常工作,但值得一提的是:您的“ def”值必须为小写。
— 杰罗姆(Jerome)

9

如果要多个目标共享相同的Pod,请使用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


2

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

abstract_target 'someNameForAbstractTarget' do
  pod 'podThatIsForAllTargets'
end

target 'realTarget' do
  pod 'podThatIsOnlyForThisTarget'
end

不应该realTarget进入内部 someNameForAbstractTarget而不是外部吗?
— Shubham Bakshi

从其他答案来看,它也可以那样工作。
— 摇晃的Sayag
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.