для быстрой 4
Сначала лучше использовать это расширение:
extension URL{
func withQueries(_ queries:[String:String]) -> URL? {
var components = URLComponents(url: self, resolvingAgainstBaseURL: true)
components?.queryItems = queries.flatMap{URLQueryItem(name: $0.0, value: $0.1)}
return components?.url
}
func justQueries(_ queries:[String:String]) -> URL? {
var components = URLComponents(url: self, resolvingAgainstBaseURL: true)
components?.queryItems = queries.flatMap{URLQueryItem(name: $0.0, value: $0.1)}
return components?.url
}
}
это расширение очень полезно для обработки параметров и их значений.
второй:
let urlstring = "https://yourdomain.com"
let url = URL(string: urlstring)!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let query :[String:String] = ["username":"admin","password":"111"]
let baseurl = URL(string:urlstring)!
let postString = (baseurl.withQueries(query)!).query
request.httpBody = postString?.data(using: .utf8)
webview.load(request as URLRequest)