I'm trying to use Swift to make a GET call to a REST API, and have tried to follow numerous tutorials, but can't figure it out. Either because I cannot figure out how to translate all the Obj-C to Swift, or because half of the methods n' such are deprecated. Does anyone know how to make the call, and parse returned JSON data?
相关问题
- Angular RxJS mergeMap types
- Design RESTful service with multiple ids
- Core Data lightweight migration crashes after App
- Axios OPTIONS instead of POST Request. Express Res
- Plain (non-HTML) error pages in REST api
Here is the complete code for REST API requests using NSURLSession in swift
For POST request ...
I hope it works.
Swift 4 - GET request
Don't forget to configure App Transport Security Settings to add your domain to the exceptions and allow insecure http requests if you're hitting endpoints without using HTTPS.
You can use a tool like http://www.json4swift.com/ to autogenerate your Codeable Mappings from your JSON responses.
I think the
NSURLSession
api fits better in this situation. Because if you write swift code your project target is at least iOS 7 and iOS 7 supportsNSURLSession
api. Anyway here is the codeSwift 3.0
In swift 3.3 and 4. I crated APIManager class with two public methods. Just pass required parameter, api name and request type. You will get response then pass it to the closure.
Api Call using Model Class
create a variable and connect with model class
create a model class New -> swift class
}
then we can connect with our table view objects