4

How do I include Alamofire (a web request pod like AFNetworking) in my cocoapod source files? I have a service in my cocoapod that needs to make web requests using Alamofire, my cocoapod doesn't seem to have a podfile that I can see, so I don't know how to add the dependency to my cocoapod.

I am creating a cocoapod using pod lib create The build fails whenever I go to import Alamofire in any of my files. In a normal project, I'd just add Alamofire to my podfile, but this is a cocoapod, so I can't figure out where to add the dependency, or how to get it to build successfully.

I followed the guide here but it doesn't say anything about importing another pod into my cocoapod's files.

My directory structure looks like this:

MyLib.podspec
Example/
  MyLib example project
Pod/
  Source files for my cocoapod
2

3 Answers 3

1

If your pod depends on other pods you can define that in your pod's .podspec file. You can add dependencies there.

Have a look at RealmSwift's podspec file as an example. The RealmSwift pod has a dependency to the Realm pod. This is defined in RealmSwift.podspec:

Pod::Spec.new do |s|
  s.name                      = 'RealmSwift'
  s.version                   = `sh build.sh get-version`
  s.summary                   = 'Realm is a modern data framework & database for iOS & OS X.'
  s.description               = <<-DESC
                                The Realm database, for Swift. (If you want to use Realm from Objective-C, see the “Realm” pod.)
                                Realm is a mobile database: a replacement for Core Data & SQLite. You can use it on iOS & OS X. Realm is not an ORM on top SQLite: instead it uses its own persistence engine, built for simplicity (& speed). Learn more and get help at https://realm.io
                                DESC
  s.homepage                  = "https://realm.io"
  s.source                    = { :git => 'https://github.com/realm/realm-cocoa.git', :tag => "v#{s.version}" }
  s.author                    = { 'Realm' => '[email protected]' }
  s.requires_arc              = true
  s.social_media_url          = 'https://twitter.com/realm'
  s.documentation_url         = "https://realm.io/docs/swift/#{s.version}"
  s.license                   = { :type => 'Apache 2.0', :file => 'LICENSE' }

  # ↓↓↓ THIS IS WHERE YOU DEFINE THE DEPENDENCY TO ANOTHER POD ↓↓↓
  s.dependency 'Realm', "= #{s.version}"
  # ↑↑↑ THIS IS WHERE YOU DEFINE THE DEPENDENCY TO ANOTHER POD ↑↑↑

  s.source_files = 'RealmSwift/*.swift'

  s.prepare_command           = 'sh build.sh cocoapods-setup without-core'
  s.preserve_paths            = %w(build.sh)

  s.pod_target_xcconfig = { 'SWIFT_WHOLE_MODULE_OPTIMIZATION' => 'YES',
                            'APPLICATION_EXTENSION_API_ONLY' => 'YES' }

  s.ios.deployment_target     = '8.0'
  s.osx.deployment_target     = '10.9'
  s.watchos.deployment_target = '2.0' if s.respond_to?(:watchos)
end
2
  • 2
    Thank you. I added s.dependency 'Alamofire' to my podspec, but the project still won't build because import Alamofire fails
    – dmoss18
    Oct 18, 2015 at 17:58
  • Did your pod install the Alamofire pod when you ran pod install in you example project?
    – joern
    Oct 18, 2015 at 18:05
1

You should check out the AlamofireImage project. It uses Carthage to add the Alamofire submodule to the project. The Alamofire project is then added as a dependency to the AlamofireImage project.

The AlamofireImage.podspec also demonstrates how to add Alamofire as a dependency for CocoaPods. If you follow the AlamofireImage project structure exactly, you'll be up and running in no time. Here are some useful commands to get you going:

Cartfile

github "Alamofire/Alamofire" ~> 3.0

Checkout through Carthage

brew update
brew doctor
brew install carthage

// or

brew upgrade carthage

carthage update --no-build --use-submodules

Hopefully that helps!

0

If you have created a pod and in your .podspec file you are trying to add a dependency (like Alamofire, RealmSwift..) after that you should go to the Example/.. folder and do a pod install to make the dependencies required from the .podspec of your custom pod visible to the .swift files in your custom pod/framework.

A typical example of a pod project folder hierarchy would be:

- MyLib/
  - _Pods.xcodeproj
  - Example/ // <-- do a pod install under this folder in order to get the dependencies declared in your .podspec
    - Podfile
    - MyLib.xcodeproj
    - MyLib.xcworkspace
  - MyLib/
    - Classes/ // <-- folder with pod specific logic that also uses Alamofire
    - Assets/
  - MyLib.podspec  // <-- your podspec with dependencies (Alamofire..)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.