我正在尝试为Swift 5的登录页面创建一个登录系统。我的php文件运行正常,但是我的URLSession遇到问题。我正在考虑为此使用Alamofire,因为它适用于我的登录页面。下面的代码在let task = session.dataTask(with: request, completionHandler: { data, response, error in
} else {
//Shortcuts
let username = usernameTextfield.text!.lowercased()
let password = passwordTextField.text
//send request to sql db
let url = URL(string: "http://localhost/myApp/login.php")!
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
let body = "username=\(String(describing: username))&password\(String(describing: password))"
let session = URLSession.shared
request.httpBody = body.data(using: .utf8, allowLossyConversion: true)
let task = session.dataTask(with: request, completionHandler: { data, response, error in
// Do something...
})
错误可能是由于使用@NicolasElPapu指出的NSMutableURLRequest
。请参阅this SO帖子。