

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make an HTTP POST request on iOS App using Swift?
To make an http request in iOS we’ll make use of DataTask and sessions. We’ll create configuration, sessions, url, request, and dataTask objects. Let’s see the steps that we’ll go through.
First of all we need to create a session object, which default configuration.
let configuration = URLSessionConfiguration.default let session = URLSession(configuration: configuration)
Then we need to create an URL Request of The type we need, it can be get, post, delete or put. In this example we are seeing ”POST” type.
let url = URL(string: URLString) //let url = NSURL(string: urlString as String) var request : URLRequest = URLRequest(url: url!) request.httpMethod = "POST" request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept")
Once we have created the request object, we need to perfrom dataTask, with URL we just created above. This is how our complete dataTask method should look like now.
let dataTask = session.dataTask(with: url!) { data,response,error in guard let httpResponse = response as? HTTPURLResponse, let receivedData = data else { print("error: not a valid http response") return } switch (httpResponse.statusCode) { case 200: //success response. break case 400: break default: break } } dataTask.resume()
Now we can embed this in a function and use in our code.
func hitAPI(_for URLString:String) { let configuration = URLSessionConfiguration.default let session = URLSession(configuration: configuration) let url = URL(string: URLString) //let url = NSURL(string: urlString as String) var request : URLRequest = URLRequest(url: url!) request.httpMethod = "POST" request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept") let dataTask = session.dataTask(with: url!) { data,response,error in // 1: Check HTTP Response for successful GET request guard let httpResponse = response as? HTTPURLResponse, let receivedData = data else { print("error: not a valid http response") return } switch (httpResponse.statusCode) { case 200: //success response. break case 400: break default: break } } dataTask.resume() }
Note: You may need to allow Transport Security exceptions in your info.plist file to access some APIs.
There is no output shown with this example as an API is required to post some data.
- Related Questions & Answers
- How to make an HTTP request on iOS App using Swift?
- How to load and display an image on iOS App using Swift?
- How to display an image with rounded corners on iOS App using Swift?
- How to rotate an image in imageview by an angle on iOS App using Swift?
- How to create a WebView in an iOS App using Swift?
- How to create a Custom Dialog box on iOS App using Swift?
- How to make phone call in iOS 10 using Swift?
- How to hide the status bar in a iOS App using Swift?
- How much does it cost to make an iOS app?
- How to make the phone call in iOS 10 using Swift?
- How to send an attachment in email using Swift(ios)?
- How to integrate a facebook login in swift for iOS App?
- How do I load an image by URL on iOS device using Swift?
- How to request Location Permission at runtime on iOS
- Using post request in middleware in express