How to create and send the json data to server using swift language

前端 未结 2 760
礼貌的吻别
礼貌的吻别 2020-12-08 17:37

I\'m new to IOS development and I have started with the swift language.

I\'m trying to get the value from two text fields and convert those two text fields into json

2条回答
  •  北海茫月
    2020-12-08 18:20

    Swift 3,Swift 4 The method above is very in-efficient, use alamofire instead

    https://github.com/Alamofire/Alamofire

    get email and passwoord from textfield

    @IBAction func submitAction(sender: AnyObject) {
    let email= emailfield.text
    let password= emailfield.text
    let parameters: Parameters = [
        "email": email,
        "password": password
        ]
    
    Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters) }
    

    or here is an example

    let parameters: Parameters = [
    "foo": "bar",
    "baz": ["a", 1],
    "qux": [
        "x": 1,
        "y": 2,
        "z": 3
    ]
    ]
    
    // All three of these calls are equivalent
    Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters)
    Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: URLEncoding.default)
    Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: URLEncoding.httpBody)
    
    // HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3
    

提交回复
热议问题