0

I'm fetching two fields from text boxes in my iOS app and I want to send the following JSON object back to the server.(Using AlamoFire) When the user enters the username and password , this data should be sent as JSON object back to the server. How should I write my POST method for sending this particular JSON object? My JSON is as follows:

   { "settings": 
       {
          "username": "myUserName",
          "password": "myPassword123"
       }
   }

This is my class in Swift 3,

class logIN {

    let username: String! = nil
    let password: String! = nil

init(ssid: String, password: String) {

    let parameters: [String: String] = [
            "username": ssid as String,
            "password": password as String
        ]

    var statusCode: Int = 0
    Alamofire.request("URL_HERE", parameters: parameters)
        .responseJSON { response in
            statusCode = (response.response?.statusCode)! 
            print(String(statusCode))

        }
    }
}

This is my IBAction for the Done button , which when clicked will send the POST request.

@IBAction func doneButton(_ sender: Any) {
    let logInInstance = logIN(ssid : self.username.text!, password : self.password.text!)

}
SeaWarrior404
  • 3,811
  • 14
  • 43
  • 65

1 Answers1

1
    let loginParam: [String: String] = [
                "username": ssid as String,
                "password": password as String
            ]

    var parameters = [String:Any]()
    parameters["setting"] = loginParam

    var statusCode: Int = 0
    Alamofire.request("URL_HERE", parameters: parameters)
        .responseJSON { response in
            statusCode = (response.response?.statusCode)! 
            print(String(statusCode))

        }
    }
Yogesh Makwana
  • 448
  • 4
  • 16