Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

xcode - Configuring Cocoapods with an existing static library and iOS application

I'm having trouble getting my workspace to compile correctly with Cocoapods. There are 3 projects in the workspace, each with their own target:

  1. libPods - Cocoapods static library with all the external dependencies
  2. libCommon - My static library where I keep all my shared code (base controllers, networking code, common UI, etc)
  3. myApp - My iOS application

Both libCommon and myApp require the external dependencies from the libPods. Originally I thought it would work like this:

  1. libPods builds
  2. libCommon links against libPods and builds
  3. myApp links with libCommon and builds

In this scenario libCommon "owns" the pods, and then then myApp just links against libCommon like I've always done pre-Cocoapods... but apparently static libraries don't like to be linked with static libraries (I got a bunch of dynamic library errors). I read on a github issue somewhere that instead I should build libPods and libCommon and then myApp should link against both libraries. Right now my podfile looks something like this:

workspace 'MyApp.xcworkspace'
platform :ios, '5.0'

link_with ['Common', 'MyApp']

target 'MyApp' do
  xcodeproj 'MyApp.xcodeproj'

  pod 'AFNetworking',               '1.1.0'
  pod 'TTTAttributedLabel',         '1.6.0'
  pod 'JSONKit',                    '1.5pre'
  pod 'Reachability',               '3.1.0'
end

With this setup, myApp owns all the pods, and then in the libCommon build settings I specify the path to the pod headers. This builds OK until I attempt to use one of the classes in libCommon. Once I do that, I get one of those _OBJC_CLASS_$_Blah errors (which tells me that although the headers are available, it's still not linked properly). When I try to manually link libCommon in "Build Phases" I get a bunch of duplicate symbol errors (which leaves me to believe it's already linked?). What the heck?

What's the way to do this properly and what should my podfile look like?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

CocoaPods creates an implicit root target which by default links with the first target of the project. As you are creating another target and the link_with option is not inherited by children target definitions your set up is not working. In order to make the link_with option you can move it inside the block of the MyApp target definition.

As the Common target links with the Pods, if you link those with the MyApp it will result in the duplicate symbols error as the app links with Common. In this case you just need to make the headers available to the MyApp target. This is simple to do but there isn't a proper DSL yet so for the moment is a bit hacky as a solution (but supported).

workspace 'MyApp.xcworkspace'
platform :ios, '5.0'

target 'Common' do
  pod 'AFNetworking',               '1.1.0'
  pod 'TTTAttributedLabel',         '1.6.0'
  pod 'JSONKit',                    '1.5pre'
  pod 'Reachability',               '3.1.0'

  target 'MyApp', :exclusive => true do
    xcodeproj 'MyApp.xcodeproj'
  end
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...