2

My question is, I don't know what really happens after I use the link_with in my Podfile so I don't know when and where to use it.I have read the guide but the description is to brief to know the meaning of link_with.

After my test, I know that if I use link_with without target in my Podfile the default Pods library libPods.a will be linked to the targets quoted by the link_with.But if there is target in the Podfile the link_with looks like making no use any more.

Besides this, the official guide don't mention where to write the link_with,so when I saw link_with 'Dummy' in the target block I was very confused.

Can anybody explain when and where to write the link_with and the reason using the link_with 'Dummy' ?(I just know that it is used to solve duplicated symbols in the library)

Onee
  • 67
  • 1
  • 8

2 Answers2

7

As document says it's not meaningful to use with one target project :

If no explicit target is specified, then the Pods target will be linked with the first target in your project. So if you only have one target you do not need to specify the target to link with.

For example I have multi target application, and here is first few lines of my Podfile :

platform :ios, '9.3'

workspace 'MyApplication'
link_with 'target1', 'target2', 'target3', 'target4'

pod 'AFNetworking', '~> 2.5.4'
pod 'Reachability'
pod 'SDWebImage', '~> 3.7.2'
pod 'CocoaLumberjack'

etc.

meaningful to use here because if you don't use "target2, target3 and target4" will give errors when you build it. So thats why we use it.

Alternatively you can specify pods for each target and add shared pods for common pods like this :

platform :ios, '9.3'

workspace 'MyApplication'

def shared_pods
    pod 'AFNetworking', '~> 2.5.4'
    pod 'SDWebImage', '~> 3.7.2'
    pod 'CocoaLumberjack', '~> 2.0.0'
end

target :target1, :exclusive => true do
  shared_pods
end 

target :target2, :exclusive => true do
  shared_pods
  pod 'Mantle'
end

target :target3, :exclusive => true do
  shared_pods
  pod 'MBProgressHUD', '~> 0.9.1'
end 

target :target4, :exclusive => true do
  shared_pods
end 

is everything clear now @Yujie Ren?

Also, check here for :exclusive => true do meaning.

MGY
  • 7,245
  • 5
  • 41
  • 74
  • 1
    Excellent `def shared_pods ... end` suggestion. – SwiftArchitect Jul 01 '15 at 08:36
  • thanks a lot~And in your Podfile, if I remove the `:exclusive=>true ` of the target2 then other targets can import the Mantle library ,is it right? – Onee Jul 01 '15 at 09:19
  • you're welcome @Yujie Ren. Yes, for exclusive you understand correct. Check here for cocoapods document example : https://guides.cocoapods.org/syntax/podfile.html#target – MGY Jul 01 '15 at 09:23
  • Thank you, @Lancelot de la Mare – MGY Jul 01 '15 at 09:38
  • @Yujie Ren Is there anything I can help? – MGY Jul 01 '15 at 10:43
  • Last question~I saw a Podfile use `link_with` like this:`target 'static' do link_wth 'Dummy' end` ,is it the right method to use? – Onee Jul 01 '15 at 11:01
  • I never try but if it's work why not, but what I understand you can make same thing with shared_pods, this way will be more easy to read I guess – MGY Jul 01 '15 at 11:04
2

Question 1: link_with vs. target:

Keyword here is:

If no explicit target is specified...

target: and link_with are mutually exclusive: use one or the other.

  • Use link_with as a shortcut if all your targets use the same Pods dependencies.
  • Use target: to specify individual Pods dependencies for each target. Note: link_with keyword is ignored when 1 or more target: are defined ; target: takes precedence.

Question 2: link_with 'Dummy'

Building a static library with cocoapods

This is a workaround to prevent a Pod from being included in a Static Library, so that said Pod in not part of said Lib. By adding an actual target named 'Dummy' to your main Static Library project, and specifying that your Pods ought to be linked against 'Dummy', you fool Cocoapods into fetching the desired Pods, yet not include them in your Static Library. Related post here: Building a static library with cocoapods.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • Thanks a lot ~But I saw a Podfile use `link_with` like this:`target 'static' do link_wth 'Dummy' end `,is it the right method to use? – Onee Jul 01 '15 at 09:13