サイトマップ 連絡先 最初に戻る 戻る 次へ進む
$Date: 2018-07-07 06:49:13 +0900 (2018/07/07 (土)) $
$Revision: 1347 $

swift 3 での HTTP アクセス

HTTP アクセスの実装

このコードはプロジェクトごと https://github.com/m-tmatma/URLSessionTest.git で公開しています。

import Foundation

class Request {
    let session: URLSession = URLSession.shared
    
    // GET METHOD
    func get(url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
        var request: URLRequest = URLRequest(url: url)
        
        request.httpMethod = "GET"
        session.dataTask(with: request, completionHandler: completionHandler).resume()
    }
    
    // POST METHOD
    func post(url: URL, body: NSMutableDictionary, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) throws {
        var request: URLRequest = URLRequest(url: url)
        
        request.httpMethod = "POST"
        request.httpBody = try JSONSerialization.data(withJSONObject: body, options: JSONSerialization.WritingOptions.prettyPrinted)
        
        session.dataTask(with: request, completionHandler: completionHandler).resume()
    }
    
    // PUT METHOD
    func put(url: URL, body: NSMutableDictionary, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) throws {
        var request: URLRequest = URLRequest(url: url)
        
        request.httpMethod = "PUT"
        request.httpBody = try JSONSerialization.data(withJSONObject: body, options: JSONSerialization.WritingOptions.prettyPrinted)
        session.dataTask(with: request, completionHandler: completionHandler).resume()
    }
    
    // PATCH METHOD
    func patch(url: URL, body: NSMutableDictionary, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) throws {
        var request: URLRequest = URLRequest(url: url)
        
        request.httpMethod = "PATCH"
        request.httpBody = try JSONSerialization.data(withJSONObject: body, options: JSONSerialization.WritingOptions.prettyPrinted)
        session.dataTask(with: request, completionHandler: completionHandler).resume()
    }
    
    // DELETE METHOD
    func delete(url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
        var request: URLRequest = URLRequest(url: url)
        
        request.httpMethod = "DELETE"
        session.dataTask(with: request, completionHandler: completionHandler).resume()
    }
}
Request を使うコード
        let request: Request = Request()
        
        let url: URL = URL(string: "http://www.asahi.com")!
        
        request.get(url: url, completionHandler: { data, response, error in
            if let res = response {
                print(res)
            }
            if let dat = data {
                print(dat)
            }
            if let err = error {
                print(err)
            }
        })

App Transport Security で http アクセスを有効化する

以下画面で右クリックする

以下の画面が出るので Add Row を選択する

App Transport Security Setting を選ぶ。App Transport Security Setting で右クリックして Add Row を選ぶ。Allow Arbitrary Loads を選ぶ。Allow Arbitrary Loads を YES にする。

参考
必須化まで約2ヵ月半!App Transport Securityについて