swift: HTTP通信フレームワークAlamofireを使ってみました

概要

HTTP通信フレームワークであるAlamofireを使ってみました。

Alamofireの記事はたくさんヒットしますが、バージョンアップ(3.0)に伴いAPI仕様が変わっています。できるだけ、開発サイト(Github)Alamofire/Alamofireを参照した方が良いです。

セットアップ

Alamofire/Alamofireによると、パッケージマネージャーあるいは手動でフレームワークを追加します。パッケージマネージャーは、CocoapodsあるいはCarthageを使います。

パッケージマネージャーを使う方法です。

Cocoapods

cocoapodsをインストールします。

$ gem install cocoapods
Successfully installed cocoapods-0.39.0
1 gem installed

Pod fileを修正します。

$ cat Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'Alamofire', '~> 3.0'

Alamofireをインストールします。

$ pod install
Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Installing Alamofire (3.1.3)
Generating Pods project
Integrating client project

xcworkspaceファイルを使ってプロジェクトを開きます。

Carthage

carthageをインストールします。

$ brew install carthage
==> Downloading https://homebrew.bintray.com/bottles/carthage-0.11.el_capitan.bo
######################################################################## 100.0%
==> Pouring carthage-0.11.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/carthage/0.11: 29 files, 11M

Cartfileを編集します。

$ cat Cartfile
github "Alamofire/Alamofire" ~> 3.0

Alamofireをインストールします。

$ carthage update
*** Cloning Alamofire
*** Checking out Alamofire at "3.1.3"
*** xcodebuild output can be found in /var/folders/bm/gfw2hl4d3zv5f29zchj4p_z40000gn/T/carthage-xcodebuild.lIlTDv.log
*** Building scheme "Alamofire OSX" in Alamofire.xcworkspace
2015-12-14 05:05:18.727 xcodebuild[8266:345298] [MT] PluginLoading: Required plug-in compatibility UUID F41BD31E-2683-44B8-AE7F-5F09E919790E for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/VVDocumenter-Xcode.xcplugin' not present in DVTPlugInCompatibilityUUIDs
...

ビルドしたAlamofire.frameworkをプロジェクトにドラッグします。

詳しい説明が、Carthage を使ってライブラリを管理する - xykのブログにあります。

手動でフレームワークを埋め込む方法

$ git init

Alamofireをサブモジュールとして追加します。

$ git submodule add https://github.com/Alamofire/Alamofire.git

XcodeでAlamofireフレームワークを追加します。

画像付きのの詳細な手順が、[Swift] HTTP通信OSS Alamofire 導入編にあります。

  1. AlamofireをFinderで開き、Alamofire.xcodeprojをXcodeのProject Navigatorにドラッグします。
  2. Project Navigatorでapplication projectを押下し、「General」タブを選択します。
  3. 「Embedded Binaries」セクションで+ボタンを押下します。
  4. Alamofire.frameworkを追加します。

使い方

Alamofireをimportします。

適当な場所(例:viewDidLoad)に処理を追加します。

詳しい使い方は、Alamofire/Alamofireを参照してください。

import Alamofire

override func viewDidLoad() {
	Alamofire.request(.GET, "https://www.google.com")
    	 .responseJSON { response in
             print(response.request)  // original URL request
             print(response.response) // URL response
         }
}

出力

Optional( { URL: https://www.google.com })
Optional( { URL: https://www.google.co.jp/?gfe_rd=cr&ei=48ptVuDgK-XZ8AeO-KfIDQ } { status code: 200, headers {
    "Cache-Control" = "private, max-age=0";
    "Content-Encoding" = gzip;
    "Content-Length" = 7919;
    "Content-Type" = "text/html; charset=Shift_JIS";
    Date = "Sun, 13 Dec 2015 19:45:39 GMT";
    Expires = "-1";
    Server = gws;
    "alt-svc" = "quic=\"www.google.com:443\"; ma=600; v=\"30,29,28,27,26,25\"";
    "alternate-protocol" = "443:quic,p=0";
    "x-frame-options" = SAMEORIGIN;
    "x-xss-protection" = "1; mode=block";
} })

関連資料