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.
Cocoapods and SPM use different folder structures, e.g. the paths for unit tests, assets, example projects, etc. are not the same. That's the main reason why supporting both CocoaPods and the Swift Package Manager is not straightforward and requires some adjustments.
Unfortunately, swift-atomics doesn't support CocoaPods, so you'll need your own Podspec for it.
After you have initially installed CocoaPods into your project, you can add new dependencies (or remove unused ones) by editing the Podfile. Then simply run pod install again.
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:
s.dependency 'RxSwift', '~> 3.0.1'
.pod install
in the Example App's folder.import RxSwift
line.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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With