Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between podspec dependency and Podfile pod declaration

I have a project for a cocoapod (let's call it "Main") which has a dependency with another cocoapod (let's call this one "Util").

I can add the Util in Main's Podfile via "pod 'Util'" and specify it as a dependency in the podspec via "s.dependency 'Util'"

As long as at least one of these are written, my test project for the pod runs fine (altho without the dependency in the podspec, running the "pod lib lint" command returns errors)

Specifying the pod in the Podfile allows me a lot more granularity as to how precise I want the version to be, even allowing me to use :head as a target. However, the dependency seems much more limited, requiring tags.

I would like to know the difference between these two approaches and if there are good practices to use when one pod depends on another (especially since I'd want to use Main as a pod for other projects eventually)

like image 465
Godfather Avatar asked Jan 30 '15 16:01

Godfather


People also ask

What is the difference between Podspec and Podfile?

You could say Podfile is your project configuration, and Podspec is your library configuration.

What's Podspec?

A Podspec, or Spec, describes a version of a Pod library. One Pod, over the course of time, will have many Specs. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, and description.

What is Podfile used for?

The Podfile is a specification that describes the dependencies of the targets of one or more Xcode projects. The file should simply be named Podfile . All the examples in the guides are based on CocoaPods version 1.0 and onwards. If you want multiple targets to share the same pods, use an abstract_target .


1 Answers

You could say Podfile is your project configuration, and Podspec is your library configuration.

Podfile:

  • Present in the root directory, as file called Podfile
  • All applications which want to use Cocoapods (e.g. add dependencies via pod 'library_name') need to have a Podfile. This is where that information goes.

Podspec (Specification of Pod library):

  • Present in the root directory, as file called Library_name.podspec, which is formatted with Ruby DSL syntax
  • This file is required to upload your library to Cocoapods.org (or use it via pod 'library_name'). When you pod trunk push, you are pushing the JSON version of this file, e.g. LibName.podpsec.json, for example, here.
  • Libraries/ packages can also specify a Podfile on top of the Podspec, for specific code which the developers don't want to accessible when downstream users use the library.
  • It has version limitations because to give the decision of selecting versions to the application which uses the library (the Podfile)

Differences:

In your case, you just have code which you're not sharing with anyone. Therefore, it doesn't need to be a library, you can just have everything in your Podfile as a separate target, and you don't even need a Podspec. If you want to share the library with others via Cocoapods, then you need the Podspec file. But, if you want to re-use main across different projects, then the configuration you want to share with those projects should be in the .podspec file, and you can reference the local directory containing the podspec in your other repos.

More information about Podspec in the documentation.

A specification describes a version of Pod library. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, and description.

A note for future readers

I would suggest you release your package/ library through Swift Package Manager instead of Cocoapods or Carthage. All the benefits of Cocoapods can be achieved with SPM. Users can easily migrate to using Swift Packages in their Xcode project because it is built-in, so there is no need to support other package managers.

like image 74
Ben Butterworth Avatar answered Sep 17 '22 13:09

Ben Butterworth