Ios/swift/外部ライブラリ/Alamofire
提供: 初心者エンジニアの簡易メモ
Alamofireとは
httpに非同期でアクセスできるライブラリ
インストール
$ vi Podfile platform :ios, '9.0' target 'Helloworld' do use_frameworks! pod 'Alamofire', '~> 3.0' end
$ pod install
使い方
import Alamofire // json Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"]).responseJSON { response in print(response.request) // original URL request print(response.response) // URL response print(response.result) // result of response serialization if let JSON = response.result.value { print("JSON: \(JSON)") } if response.result.isSuccess { let jsonDic = response.result.value as! NSDictionary let url = jsonDic["url"] as! String let headers = jsonDic["headers"] as! NSDictionary print(url); print(headers["Host"]) for (key, value) in headers { print(key) print(value) } } } // str Alamofire.request(.GET, "ttps://example.com/get", parameters: ["foo": "bar"]).responseString { response in if let str = response.result.value { print("str: \(str)") } }