首页 文章

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

提问于
浏览
119

我在我的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 回答

  • 1

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

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

    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
    
  • 78

    我认为更好的解决方案是

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

  • 4

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

相关问题