facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(使い方)
行14: 行14:
 
==使い方==
 
==使い方==
 
  import Alamofire
 
  import Alamofire
         Alamofire.request(.GET, "https://twitter.com")
+
// json
            .responseJSON { response in
+
         Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"]).responseJSON { response in
                print(response.request)
+
            print(response.request) // original URL request
                print(response.response)
+
            print(response.response) // URL response
 +
            print(response.result)  // result of response serialization
 +
            if let JSON = response.result.value {
 +
                print("JSON: \(JSON)")
 +
            }
 +
        }
 +
// 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
Optional(<NSMutableURLRequest: 0x15de5ce90> { URL: https://twitter.com })
+
Optional(<NSHTTPURLResponse: 0x15de03170> { URL: https://twitter.com/ } { status code: 200, headers {
+
    "Cache-Control" = "no-cache, no-store, must-revalidate, pre-check=0, post-check=0";
+
    "Content-Encoding" = gzip;
+
    "Content-Length" = 32873;
+
    "Content-Type" = "text/html;charset=utf-8";
+
    Date = "Thu, 23 Jun 2016 03:08:19 GMT";
+
    Expires = "Tue, 31 Mar 1981 05:00:00 GMT";
+
    "Last-Modified" = "Thu, 23 Jun 2016 03:08:19 GMT";
+
    Pragma = "no-cache";
+
    Server = "tsa_a";
+
    "Set-Cookie" = "fm=0; Expires=Thu, 23 Jun 2016 03:08:09 GMT; Path=/; Domain=.twitter.com; Secure; HTTPOnly, _twitter_sess=BAh7CSIKZmxhc2hJQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrCAHToMY3NyZl9p%250AZCIlZGMzNTBjMWZiMGI5MmZlNmM1MDVmMmMyNTNjZDVjYjM6B2lkIiU0MzA2%250AN2ZiMDgzYWY4YTYyYTk5YTdiYjRmNQ%253D%253D--49410ad03124cde2eab339902b9f116860f4; Path=/; Domain=.twitter.com; Secure; HTTPOnly, guest_id=v1%3A146658349991; Domain=.twitter.com; Path=/; Expires=Sat, 23-Jun-2018 03:08:19 UTC";
+
    Status = "200 OK";
+
    "Strict-Transport-Security" = "max-age=631138519";
+
    "x-connection-hash" = a80a35cb815d23f423a94ea3bb30;
+
    "x-content-type-options" = nosniff;
+
    "x-frame-options" = SAMEORIGIN;
+
    "x-response-time" = 157;
+
    "x-transaction" = 00d8e8bec4f0;
+
    "x-twitter-response-tags" = BouncerCompliant;
+
    "x-ua-compatible" = "IE=edge,chrome=1";
+
    "x-xss-protection" = "1; mode=block";
+
} })
+
 
+
参考:http://unokun.hatenablog.jp/entry/2015/12/14/060929
+

2016年6月24日 (金) 05:16時点における版

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)")
           }
       }
// 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