如何将值传递给Swift中的API?

时间:2017-04-03 04:39:31

标签: ios swift

我几乎是Swift的初学者,我试图传递一个值来调用API,其值来自输入字段。 API如下所示。如何编写代码以从文本字段传递ZIP_CODE的值?

https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/ZIP_CODE/JSON

@IBOutlet var zipLabel: UILabel!
@IBOutlet var zipInput: UITextField!
@IBOutlet var jsondataLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    getJSON()      
}

@IBAction func btnAction() {
    zipLabel.text = zipInput.text
}

func getJSON(){
    let url = NSURL(string :"https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/10001/JSON")
    let request = URLRequest(url: url as! URL )
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    let task = session.dataTask(with: request){(data, response, error ) -> Void in
        if error == nil{
            DispatchQueue.main.async(execute: {
                let jsondata = JSON(data : data!)

                print(jsondata)
                print("-----")
                print(jsondata[0]["UV_INDEX"])

                let result =  jsondata[0]["UV_INDEX"].stringValue

                self.jsondataLabel.text = result
            })
        }else{
            print("There was an error")    
        }
    }
    task.resume()
}

2 个答案:

答案 0 :(得分:1)

func getJSON(_ zipcode : Int){



            let url = NSURL(string :"https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/\(zipcode)/JSON")

            let request = URLRequest(url: url as! URL )

            let config = URLSessionConfiguration.default

            let session = URLSession(configuration: config)



            let task = session.dataTask(with: request){(data, response, error ) -> Void in

                if error == nil{

                    DispatchQueue.main.async(execute: {

                        let jsondata = JSON(data : data!)

                        print(jsondata)

                        print("-----")

                        print(jsondata[0]["UV_INDEX"])

                        let result =  jsondata[0]["UV_INDEX"].stringValue

                        self.jsondataLabel.text = result
                    })

                }else{

                    print("There was an error")    
                }

            }
            task.resume()
    }

答案 1 :(得分:0)

如果您的API为您提供GET请求的回复,您可以使用以下代码 -

@IBOutlet var zipLabel: UILabel!
@IBOutlet var zipInput: UITextField!
@IBOutlet var jsondataLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func btnAction() {
    zipLabel.text = zipInput.text
    getJSON(zipcode: zipInput.text)
}

func getJSON(zipcode: String){
    let url = NSURL(string :"https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/10001/JSON&zipcode="+zipcode)
    let request = URLRequest(url: url as! URL )
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    let task = session.dataTask(with: request){(data, response, error ) -> Void in
        if error == nil{
            DispatchQueue.main.async(execute: {
                let jsondata = JSON(data : data!)

                print(jsondata)
                print("-----")
                print(jsondata[0]["UV_INDEX"])

                let result =  jsondata[0]["UV_INDEX"].stringValue

                self.jsondataLabel.text = result
            })
        }else{
            print("There was an error")    
        }
    }
    task.resume()
}

但请注意,如果您的API为POST请求提供响应,则此代码将无效。