如何在我的自定义cocoapod中导入Alamofire / AFNetworking

时间:2015-10-18 06:09:22

标签: ios swift afnetworking cocoapods alamofire

如何在我的cocoapod源文件中包含Alamofire(像AFNetworking这样的网络请求窗格)?我在我的cocoapod中有一项服务需要使用Alamofire进行网络请求,我的cocoapod似乎没有我可以看到的podfile,所以我不知道如何将依赖项添加到我的cocoapod。

我正在使用pod lib create创建一个cocoapod每当我在任何文件中导入Alamofire时,构建都会失败。在正常的项目中,我只是将Alamofire添加到我的podfile中,但这是一个cocoapod,所以我无法找出添加依赖项的位置,或者如何使其成功构建。

我按照指南here进行了操作,但没有说明将其他广告单元导入我的cocoapod文件中。

我的目录结构如下所示:

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

3 个答案:

答案 0 :(得分:1)

如果您的广告连播取决于其他广告连播,您可以在广告连播的.podspec文件中定义该广告连播。您可以在那里添加依赖项。

以RealmSwift的podspec文件为例。 RealmSwift pod与Realm pod有依赖关系。这在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' => 'help@realm.io' }
  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

答案 1 :(得分:0)

您应该查看AlamofireImage项目。它使用Carthage将Alamofire子模块添加到项目中。然后将Alamofire项目添加为AlamofireImage项目的依赖项。

AlamofireImage.podspec还演示了如何将Alamofire添加为CocoaPods的依赖项。如果您完全遵循AlamofireImage项目结构,您将立即启动并运行。以下是一些有用的命令:

Cartfile

github "Alamofire/Alamofire" ~> 3.0

通过迦太基结账

brew update
brew doctor
brew install carthage

// or

brew upgrade carthage

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

希望这有帮助!

答案 2 :(得分:0)

如果您创建了pod,并且在.podspec文件中尝试添加dependency(例如Alamofire,RealmSwift ..),则应转到Example/..文件夹并 执行一个pod install,以使自定义pod的.podspec中所需的依赖项可见于其中的.swift文件您的自定义广告连播/框架。

Pod项目文件夹层次结构的典型示例为:

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