使Swift类发出Http请求

时间:2017-05-22 21:07:36

标签: ios swift http

我在下面创建了这个类来帮助从我的应用程序中的任何地方发出http请求,但是当我调用该类时它不会按预期发出请求。我看到我可以从功能打印,所以我知道它至少可以实现这一目标。我需要做什么才能提出这个要求? api参数也正确传递。

viewController中的

let url = "post/test"
MakeHttpRequest.sharedInstance.postRequest(api: url)
课堂上的

import Foundation

final class MakeHttpRequest {
    static let sharedInstance = MakeHttpRequest()
    var api_token = "token_here"
    var url_base = "https://test.frb.io/"
    func postRequest (api : String) {
        let parameters = ["email": "testing123@gmail.com"]
        guard let destination = URL(string: url_base + api) else { return }
        var request = URLRequest(url: destination)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        //            request.setValue("clientIDhere", forHTTPHeaderField: "Authorization")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: [])
            else { return }
        request.httpBody = httpBody
        let session = URLSession.shared
        session.dataTask(with: request) { (data, response, error) in
            if let data = data {
                do {
                    let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    if (json["response"]) != nil {

                    }
                } catch {
                    print(error)
                }
            }
        }.resume()
        print(url_base + api)
    }
    private init() {
    }
}

1 个答案:

答案 0 :(得分:0)

所以要检查两件事。有时快速的功能链不能正常工作。因此,尝试将session.dataTask和resume拆分为两个语句。第二步尝试向响应添加错误处理。您可能会在不知情的情况下收到错误消息。请参阅下面的代码示例。

import Foundation

final class MakeHttpRequest {
    static let sharedInstance = MakeHttpRequest()
    var api_token = "token_here"
    var url_base = "https://test.frb.io/"
    func postRequest (api: String, parameters: [String: Any]? = nil) {
        guard let destination = URL(string: url_base + api) else { return }
        var request = URLRequest(url: destination)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        //            request.setValue("clientIDhere", forHTTPHeaderField: "Authorization")
        if let parameters = parameters {
            guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return
            }
            request.httpBody = httpBody
        }
        let session = URLSession.shared
        let task = session.dataTask(with: request) { (data, response, error) in
            if let data = data {
                do {
                    let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    if (json["response"]) != nil {
                        print("1234")
                    } else {
                        print("ABCD")
                    }
                } catch {
                    print(error)
                }
            } else {
                print(error ?? "")
            }
        }
        task.resume()
        print(url_base + api)
    }

    private init() {
    }
}

let url = "post/test"
MakeHttpRequest.sharedInstance.postRequest(api: url)