如何在我的 podfile 中为我的 Xcode 项目指定多个目标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14906534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I specify multiple targets in my podfile for my Xcode project?
提问by Austin
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:
我在我的 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'
The only way I was get this to work was to specify each target individually with all the the pods listed again.
我让它工作的唯一方法是用再次列出的所有 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
Is there a better way to do this?
有一个更好的方法吗?
回答by Keith Smiley
CocoaPods 1.0 has changed the syntax for this. It now looks like this:
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
OUTDATEDPre CocoaPods 1.0 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.
是的,有更好的方法!查看link_with
您可以在哪里link_with 'MyApp', 'MyOtherApp'
指定多个目标。
I use this with unit tests like link_with 'App', 'App-Tests'
(beware of spaces in target's names).
我将它用于单元测试,例如link_with 'App', 'App-Tests'
(注意目标名称中的空格)。
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'
2017 update
2017年更新
You can use abstract_target
您可以使用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
回答by Adarsh G J
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/
参考:http: //natashatherobot.com/cocoapods-installing-same-pod-multiple-targets/
回答by Adam Smaka
If you want multiple targets to share the same pods, use an abstract_target.
如果您希望多个目标共享相同的 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
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
回答by Shaked Sayag
Easiest way is to use an abstract target, where each pod specified will be linked with all targets.
最简单的方法是使用抽象目标,其中指定的每个 pod 将与所有目标链接。
abstract_target 'someNameForAbstractTarget' do
pod 'podThatIsForAllTargets'
end
target 'realTarget' do
pod 'podThatIsOnlyForThisTarget'
end