LoginSignup
17
16

More than 5 years have passed since last update.

Swift - http通信処理

Last updated at Posted at 2015-08-25

SwiftでのHTTP通信処理の例です。

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //urlを生成
        var url:NSURL = NSURL(string:"http://xxx.com")!

        //リクエストを生成
        var request:NSURLRequest  = NSURLRequest(URL: url)

        //リクエストを投げる self.getHttpはレスポンス取得後の処理
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: self.getHttp)

    }

    //レスポンス取得後の処理
    func getHttp(res:NSURLResponse?,data:NSData?,error:NSError?){
        var response:NSString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
        println(response)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/13-http/fei-tong-qihttp を参照)

17
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
16