19

I'm creating a CocoaPod, say MyPod, which depends on another Cocoapod, say RxSwift.

So I have this in MyPod.podspec:

s.dependency "RxSwift", "~> 3.0.1"

However, while developing MyPod, how can I actually use the dependency?

import RxSwift
//     ^
// No such module 'RxSwift'

public class MyClass { //...

Is there a step I'm missing, or some common convention? It looks like some other projects like Moya are using Carthage to build dependencies while developing. Should I be doing that, or maybe adding a Podfile?

I know that this shouldn't be a problem for an Example App located within the repo, which would have its own Podfile. However, I'd like to still have tests located at top level, outside of the Example App, and to be able to actually build the framework while working on it, again, outside of an Example App.

3 Answers 3

14

I can't speak to whether or not to use CocoaPods or Carthage. Both have their strong points and weak points. Plus the decision should be made considering many factors, some of which you might not be able to control (like a client that insists you use CocoaPods!) So I'll skip that part.

However, to your question, indeed a pod you are developing can depend on another pod. You already have the correct s.dependency line. That's necessary.

However, I suspect that the reason why you were not able to reference the dependent pod could be because you did not have a Podfile in your 'tester/example' project and/or you did not do a pod install after adding the dependency in your Podspec.

The reason for this is requirement I suspect is that since the Podspec is not actually processed at all by Xcode, you're not actually downloading (or compiling) the dependency.

Instead, when you do the pod install (via command line of course), CocoaPods will create a Pods project with your development pod, the pods you depend on (in Podspec) as well as any other pods in your Podfile.

To test this theory, I:

  • Created a new pod (using CocoaPod's own 'pod lib create' (https://guides.cocoapods.org/making/using-pod-lib-create.html).
  • Opened the workspace that CocoaPod created for me and edited the Podspec to add the dependency s.dependency 'RxSwift', '~> 3.0.1'.
  • Added another pod in my Example App's Podfile (to demonstrate the difference between Podfile dependencies and Podspec dependencies.)
  • Performed pod install in the Example App's folder.
  • Edited my Pod's class to do something useful AND to add the import RxSwift line.
  • Added a label to my Example App ("Hello World" of course).
  • Used PureLayout to do all the auto layout constraints for the label (and to demonstrate how the Example project has access to both pods - the development pod as well as the referenced pod PureLayout.)

You can check out the demo I created on my public GitHub: https://github.com/ericwastaken/CocoaPod-Dependency-Demo

Honestly, I've created several pods using the pod lib create and it does indeed create a nice structure that has always worked for me. For this reason, I would recommend always using it to create your pod's skeleton.

Xcode 8 comment: pod lib create still seems to create a Swift 1.x project. So, right after you use this tool, when you open Xcode, you'll be offered to "convert" to a newer version of Swift. I would let that conversion happen right then and there (the first time) so that you can be in Swift 2.x or 3.x syntax (you choose).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the thorough answer. However, I think you missed the last part of my question, where I say I don't want to have to develop through the ExampleApp (or even have one at all) and I want to have my tests at top-level in the framework itself. I want my ExampleApp (if I even have one) to be there simply to show how the framework can be used. It should not contain anything essential, like tests. It should be able to be removed without worry.
I encourage you to clone the repo I created. You'll notice that the Example App is not needed for anything at all and it can indeed be removed. You do all development in the 'Development Pods' just like you state. In addition you can indeed write unit tests against the pod. The unit test target can simply have the pod as dependency and you can use TESTABLE to expose private methods into the tests. Feel free to clone it and experiment with removing the Example. Note, you still do need a Podfile but that could be by itself.
Yea so in the end, the main take-away is that you still need an extra Podfile for the framework to be able to build itself. As for your repo, I tried building it, but I'm getting a ton of Xcode issues with it. Files not found (red), then it adding a build folder at ../build when I try to build. If I close and open it again, it creates a copied scheme for every scheme: "___ 2" for each. It's fine though, since I've already come to the same solution as you (the Podfile), project setup differences aside.
That's correct. Since the only way to 'process' the dependency is by running pod install, you indeed need the poodfile. No way around that for sure. I'll double check the repo to ensure it clones and builds correctly. Glad you have some resolution.
4

I ended up using Carthage to build the framework dependencies. I imagine I could have used CocoaPods to do it as well. However, that would have required that I start using a workspace, and I didn't want to have to do that so as to keep changes as minimal as possible.

Also, with Carthage, it didn't require that I add a new Podfile/Podfile.lock, since Carthage will use the existing Cartfile/Cartfile.resolved that's already there. That's because Carthage uses the Cartfile.resolved when using the framework in another project and when building the framework on its own. Whereas, with CocoaPods, *.podspec is used when using the framework in another project but Podfile.lock (if you've added a Podfile) is required to install dependent pods in the framework itself.

Comments

0

This was a very challenging problem to solve, and required a combination of a few solutions pieced together. @EricWasTaken's solution helped, as well as adding:

source 'https://github.com/CocoaPods/Specs.git'

to the top of my Podfile. Then navigating to the Example app and run

pod repo update
pod install

Now the framework I am creating can find the cocoapods my framework requires.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.