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

时间:2013-02-16 03:16:42

标签: xcode target cocoapods

我在我的Xcode 4项目中使用CocoaPods,我的项目有三个目标(默认情况下,一个用于构建精简版本,一个用于构建演示版本)。所有目标都使用相同的库,但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

有更好的方法吗?

4 个答案:

答案 0 :(得分:300)

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

OUTDATED Pre 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

答案 1 :(得分:81)

我认为更好的解决方案是

# 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

参考文献:Fitting to datetime with y values

答案 2 :(得分:6)

如果您希望多个目标共享相同的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

答案 3 :(得分:1)

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

abstract_target 'someNameForAbstractTarget' do
  pod 'podThatIsForAllTargets'
end

target 'realTarget' do
  pod 'podThatIsOnlyForThisTarget'
end