facebook twitter hatena line email

「Ios/swift/外部ライブラリ/Alamofire」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(画像API例)
(画像API例)
行44: 行44:
  
 
==画像API例==
 
==画像API例==
-testimgs.html
+
-php
 +
$ret = array(
 +
  'cnt' => 2,
 +
  'imgs' => array(
 +
    array(
 +
      'thumbnail' => "https://pbs.twimg.com/media/ClupGSKWEAAeOuy.jpg",
 +
      'name' => "オバマさん",
 +
    ),
 +
    array(
 +
      'thumbnail' => "https://pbs.twimg.com/media/ClfZX4sWEAEeJzB.jpg",
 +
      'name' => "ドロイド君",
 +
    ),
 +
  ),
 +
);
 +
echo json_encode($ret);
 +
// 生成された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"}]}
 
  {"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"}]}
  

2016年6月25日 (土) 05:13時点における版

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例

-php

$ret = array(
 'cnt' => 2,
 'imgs' => array(
   array(
     'thumbnail' => "https://pbs.twimg.com/media/ClupGSKWEAAeOuy.jpg",
     'name' => "オバマさん",
   ),
   array(
     'thumbnail' => "https://pbs.twimg.com/media/ClfZX4sWEAEeJzB.jpg",
     'name' => "ドロイド君",
   ),
 ),
);
echo json_encode($ret);
// 生成された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"}]}

-ImgInit.swift

class ImgInit {
   var name:String = ""
   var thumbnail:String = ""
   init(name: String, thumbnail: String) {
       self.name = name
       self.thumbnail = thumbnail
   }
}

-SampleController.swift

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)