I\'ve got to send a https GET request to a web service in my iPhone app which is developing in Swift 1.2.
I am trying to construct query string parameters but got to
You should use NSURLComponents for your task.
Given a URL string, create a url-components:
let urlString = "http://example.com"
let urlComponents = NSURLComponents(string: urlString)!
Given a query parameter container (possibly a dictionary, or an array of (String, String?) tuple), create an array of NSURLQueryItems:
let queryParameters: [String: String?] = ["param": "az09-._~!$&'()*+,;=:@/?", "reserved": ":/?#[]@!$&'()*+,;="]
var queryItems = queryParameters.map { NSURLQueryItem(name: $0.0, value: $0.1) }
Append the query-component to the url-components:
urlComponents.queryItems = queryItems.count > 0 ? queryItems : nil
print(urlComponents.string!)
prints:
http://example.com?reserved=:/?%23%5B%5D@!$%26'()*+,;%3D¶m=az09-._~!$%26'()*+,;%3D:@/?