Swift http请求使用urlSession [英] Swift http request use urlSession

查看:144
本文介绍了Swift http请求使用urlSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的服务器编写HTTP请求的func并获取一些数据,当我打印它(print(responseString))它看起来不错,但是当我尝试返回数据时,它总是空的

I want to write func for HTTP Request to my server and get some data, when i print it (print(responseString)) it looks good, but when i try to return data, its always empty

public func HTTPRequest(dir: String, param: [String:String]?) -> String{
    var urlString = HOST + dir + "?"
    var responseString = ""
    if param != nil{
        for currentParam in param!{
            urlString += currentParam.key + "=" + currentParam.value + "&"
        }
    }

    let url = URL(string: urlString)

    let task = URLSession.shared.dataTask(with: url!) { data, response, error in
        guard error == nil else {
            print("ERROR: HTTP REQUEST ERROR!")
            return
        }
        guard let data = data else {
            print("ERROR: Empty data!")
            return
        }
        responseString = NSString(data: data,encoding: String.Encoding.utf8.rawValue) as! String
        print(responseString)
    }
    task.resume()
    return responseString
}


推荐答案

您需要使用完成块而不是返回值,因为 dataTask 闭包从您的方法返回后,异步运行,即稍后运行。您不想尝试立即返回该值(因为您还没有它)。你想(a)改变这个函数不返回任何东西,但是(b)提供一个完成处理程序闭包,你将在dataTask闭包中调用它来构建responseString。

You need to use completion block instead of returning value because the dataTask closure is run asynchronously, i.e. later, well after you return from your method. You don't want to try to return the value immediately (because you won't have it yet). You want to (a) change this function to not return anything, but (b) supply a completion handler closure, which you will call inside the dataTask closure, where you build responseString.

例如,您可以这样定义:

For example, you might define it like so:

public func HTTPRequest(dir: String, param: [String:String]? = nil, completionHandler: @escaping (String?, Error?) -> Void) {
    var urlString = HOST + dir

    if let param = param {
        let parameters = param.map { return $0.key.percentEscaped() + "=" + $0.value.percentEscaped() }
        urlString += "?" + parameters.joined(separator: "&")
    }

    let url = URL(string: urlString)

    let task = URLSession.shared.dataTask(with: url!) { data, response, error in
        guard let data = data, error == nil else {
            completionHandler(nil, error)
            return
        }
        let responseString = String(data: data, encoding: .utf8)
        completionHandler(responseString, nil)
    }
    task.resume()
}

注意,我是使用以下内容转义参数字典中的值的百分比:

Note, I'm percent escaping the values in the parameters dictionary using something like:

extension String {

    /// Percent escapes values to be added to a URL query as specified in RFC 3986
    ///
    /// This percent-escapes all characters besides the alphanumeric character set and "-", ".", "_", and "~".
    ///
    /// http://www.ietf.org/rfc/rfc3986.txt
    ///
    /// - Returns: Returns percent-escaped string.

    func percentEscaped() -> String {
        let allowedCharacters = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~")

        return self.addingPercentEncoding(withAllowedCharacters: allowedCharacters)!
    }

}

然后你就叫它像这样:

HTTPRequest(dir: directory, param: parameterDictionary) { responseString, error in
    guard let responseString = responseString else {
        // handle the error here
        print("error: \(error)")
        return
    }

    // use `responseString` here

    DispatchQueue.main.async {
        // because this is called on background thread, if updating
        // UI, make sure to dispatch that back to the main queue.
    }
}

// but don't try to use `responseString` here

这篇关于Swift http请求使用urlSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆