「Ios/swift/外部ライブラリ/Alamofire」の版間の差分
提供: 初心者エンジニアの簡易メモ
行42: | 行42: | ||
参考:http://llcc.hatenablog.com/entry/2015/09/29/235112 | 参考:http://llcc.hatenablog.com/entry/2015/09/29/235112 | ||
+ | |||
+ | ==画像API例== | ||
+ | -api.html | ||
+ | {"cnt":2,"imgs":[{"thumbnail":"https:\/\/pbs.twimg.com\/media\/ClupGSKWEAAeOuy.jpg","name":"\u30aa\u30d0\u30de\u3055\u3093"},{"thumbnail":"https:\/\/pbs.twimg.com\/media\/ClfZX4sWEAEeJzB.jpg","name":"\u30c9\u30ed\u30a4\u30c9\u541b"}]} | ||
+ | |||
+ | -swift | ||
+ | class ImgInit { | ||
+ | var name:String = "" | ||
+ | var thumbnail:String = "" | ||
+ | init(name: String, thumbnail: String) { | ||
+ | self.name = name | ||
+ | self.thumbnail = thumbnail | ||
+ | } | ||
+ | } | ||
+ | Alamofire.request(.GET, "http://localhost/api/testimgs", parameters: ["param1": "value1"]).responseJSON { response in | ||
+ | if response.result.isSuccess { | ||
+ | let jsonDic = response.result.value as! NSDictionary | ||
+ | let imgs = jsonDic["imgs"] as! NSArray | ||
+ | let cnt = jsonDic["cnt"] as! Int | ||
+ | print(cnt) | ||
+ | var _imgs: Array<ImgInit> = [] | ||
+ | for img in imgs { | ||
+ | let _img = ImgInit(name: String(img["name"]), thumbnail: String(img["thumbnail"])) as ImgInit | ||
+ | _imgs.append(_img) | ||
+ | } | ||
+ | // こんな感じで取得 | ||
+ | for _img in _imgs { | ||
+ | print(_img.name) | ||
+ | print(_img.thumbnail) | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | -出力 | ||
+ | Optional(オバマさん) | ||
+ | Optional(https://pbs.twimg.com/media/ClupGSKWEAAeOuy.jpg) | ||
+ | Optional(ドロイド君) | ||
+ | Optional(https://pbs.twimg.com/media/ClfZX4sWEAEeJzB.jpg) |
2016年6月25日 (土) 05:09時点における版
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)") } }
参考:http://llcc.hatenablog.com/entry/2015/09/29/235112
画像API例
-api.html {"cnt":2,"imgs":[{"thumbnail":"https:\/\/pbs.twimg.com\/media\/ClupGSKWEAAeOuy.jpg","name":"\u30aa\u30d0\u30de\u3055\u3093"},{"thumbnail":"https:\/\/pbs.twimg.com\/media\/ClfZX4sWEAEeJzB.jpg","name":"\u30c9\u30ed\u30a4\u30c9\u541b"}]}
-swift
class ImgInit { var name:String = "" var thumbnail:String = "" init(name: String, thumbnail: String) { self.name = name self.thumbnail = thumbnail } } Alamofire.request(.GET, "http://localhost/api/testimgs", parameters: ["param1": "value1"]).responseJSON { response in if response.result.isSuccess { let jsonDic = response.result.value as! NSDictionary let imgs = jsonDic["imgs"] as! NSArray let cnt = jsonDic["cnt"] as! Int print(cnt) var _imgs: Array<ImgInit> = [] for img in imgs { let _img = ImgInit(name: String(img["name"]), thumbnail: String(img["thumbnail"])) as ImgInit _imgs.append(_img) } // こんな感じで取得 for _img in _imgs { print(_img.name) print(_img.thumbnail) } } }
-出力
Optional(オバマさん) Optional(https://pbs.twimg.com/media/ClupGSKWEAAeOuy.jpg) Optional(ドロイド君) Optional(https://pbs.twimg.com/media/ClfZX4sWEAEeJzB.jpg)