安裝或設定

安裝

CocoaPods

CocoaPods 是 Cocoa 專案的依賴管理器。你可以使用以下命令安裝它:

$ gem install cocoapods

構建 Alamofire 4.0.0+需要 CocoaPods 1.1.0+。

要使用 CocoaPods 將 Alamofire 整合到你的 Xcode 專案中,請在你的 Podfile 中指定它:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'Alamofire', '~> 4.0'
end

然後,執行以下命令:

$ pod install

迦太基

Carthage 是一個分散的依賴管理器,它構建你的依賴項併為你提供二進位制框架。

你可以使用以下命令安裝 Carthage with Homebrew

$ brew update
$ brew install carthage

要使用 Carthage 將 Alamofire 整合到你的 Xcode 專案中,請在你的 Cartfile 中指定它:

github "Alamofire/Alamofire" ~> 4.0

執行 carthage update 構建框架並將構建的 Alamofire.framework 拖入 Xcode 專案。

手動

如果你不想使用上述任何一個依賴關係管理器,則可以手動將 Alamofire 整合到你的專案中。

嵌入式框架

  • 將 Terminal,cd 開啟到你的頂級專案目錄中,並執行以下命令 if 你的專案未初始化為 git 儲存庫:
$ git init
  • 通過執行以下命令將 Alamofire 新增為 git 子模組
$ git submodule add https://github.com/Alamofire/Alamofire.git
  • 開啟新的 Alamofire 資料夾,然後將 Alamofire.xcodeproj 拖到應用程式 Xcode 專案的 Project Navigator 中。

    它應該巢狀在應用程式的藍色專案圖示下面。無論是高於還是低於所有其他 Xcode 組並不重要。

  • 在 Project Navigator 中選擇 Alamofire.xcodeproj,並驗證部署目標是否與應用程式目標的目標匹配。

  • 接下來,在 Project Navigator 中選擇你的應用程式專案(藍色專案圖示)以導航到目標配置視窗,並在側欄中的 Targets 標題下選擇應用程式目標。

  • 在該視窗頂部的標籤欄中,開啟常規面板。

  • 單擊嵌入式二進位制檔案部分下的+按鈕。

  • 你將看到兩個不同的 Alamofire.xcodeproj 資料夾,每個資料夾都有兩個不同版本的 Alamofire.framework 巢狀在 Products 資料夾中。

    你選擇哪個 Products 資料夾並不重要,但無論你選擇頂部還是底部 Alamofire.framework 都很重要。

  • 選擇適用於 iOS 的頂級 Alamofire.framework 和適用於 OS X 的底部 Alamofire.framework

    你可以通過檢查專案的構建日誌來驗證你選擇的那個。Alamofire 的構建目標將列為 Alamofire iOSAlamofire macOSAlamofire tvOSAlamofire watchOS

  • 就是這樣!

Alamofire.framework 作為目標依賴項,連結框架和嵌入式框架自動新增到複製檔案構建階段,這是構建模擬器和裝置所需的全部內容。