dmoss18
dmoss18

Reputation: 867

How to import Alamofire/AFNetworking in my custom cocoapod

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

Upvotes: 4

Views: 1439

Answers (3)

denis_lor
denis_lor

Reputation: 6547

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..)

Upvotes: 0

cnoon
cnoon

Reputation: 16663

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!

Upvotes: 1

joern
joern

Reputation: 27620

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

Upvotes: 1

Related Questions