0

I try to only be able to post one data only, and only the username data can not password. Could you please help me?????

func onlinecheckin(){
    self.password.resignFirstResponder()
    let password1 = password.text;
    if let user1 = user.text {
        let username = user1.trimmingCharacters(in: CharacterSet.whitespaces)
        let passwordnya = password1!.trimmingCharacters(in: CharacterSet.whitespaces)
        //Send user data to server side
        let myUrl = URL(string: "http://login.php")
        let request = NSMutableURLRequest(url: myUrl!);
        request.httpMethod = "POST"
        let postString = "user=\(username)"
        let postString2 = "password=\(passwordnya)"
        //To upload any text at the server.
        request.httpBody = postString2.data(using: String.Encoding.utf8)
        request.httpBody = postString.data(using: String.Encoding.utf8)
        task = URLSession.shared.dataTask(with: request as URLRequest) {data, response, error in

            print("response online checking =\(String(describing: response))")
            if error != nil {
                task.cancel()
                self.AlertMessage("Error. Please press Login button again.");
                print("error=\(String(describing: error))")
                return
            }
            // Print out downloaded data.
            if let datadownload = data {
                resultlogin = NSString(data: datadownload, encoding: String.Encoding.utf8.rawValue)!.replacingOccurrences(of: " ", with: "");
                print("result of online checking:\(resultlogin)")
            }
            DispatchQueue.main.async(execute: { () -> Void in
                if resultlogin.contains("already") {
                    let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "menu") as! bdemenu                                   self.navigationController?.pushViewController(secondViewController, animated: true)
                } else {
                    self.AlertMessage("User dan Password Salah");
                }
            })
        }
        task.resume()
    }
}

I try to only be able to post one data only, and only the username data can not password. Could you please help me?????

Rob
  • 415,655
  • 72
  • 787
  • 1,044
dante
  • 5
  • 4
  • Hey, can you please reformulate your question? Also, this might help you: https://stackoverflow.com/questions/26364914/http-request-in-swift-with-post-method – rs7 Jun 08 '20 at 04:30
  • As that link shows, you want to (a) percent escape the values; and (b) join these together with `&` into a single `httpBody`. Right now you’re setting it twice (and the first, `postString2` is getting lost). Also, I’d suggest you use `var request = URLRequest(url: ...)` rather than the old `NSMutableURLRequest`. – Rob Jun 08 '20 at 04:32
  • Also, if the task has an error, you don’t need to `cancel` the request. The request is done by that point. No need to `cancel`. – Rob Jun 08 '20 at 04:35

0 Answers0