0% found this document useful (0 votes)
84 views14 pages

Networking: Sending Urlrequest Using Urlsession

The document discusses how to send URL requests using URLSession in Swift. It explains that a URLSession handles networking operations on a background thread, sending a URLRequest containing the server URL, method, headers, and body. A data task is created to send the request and handle the response. The main steps are to create a URL object, URLRequest, URLSession, data task, send the request, and handle the response on the main thread.

Uploaded by

Lina Y Harara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views14 pages

Networking: Sending Urlrequest Using Urlsession

The document discusses how to send URL requests using URLSession in Swift. It explains that a URLSession handles networking operations on a background thread, sending a URLRequest containing the server URL, method, headers, and body. A data task is created to send the request and handle the response. The main steps are to create a URL object, URLRequest, URLSession, data task, send the request, and handle the response on the main thread.

Uploaded by

Lina Y Harara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Networking

Sending URLRequest using URLSession Mr. Ahmed Qazzaz

last update Dec. 1st., 2019


1
URL

✤ URL class used to hold a valid web URL link

✤ Simply create URL object from String

✤ Example

2
URLRequest

✤ URLRequest class will contain all the request information

✤ The server URL

✤ Method (GET, POST, PATCH, PUT, DELETE)

✤ Headers (e.g. Content-Type, Authentication …)

✤ Body

✤ … etc
3
URLSESSION

✤ Session is the class that is responsible of sending the


request and waiting for the response (i.e. opening a
background thread to handle the networking operations)

✤ Session can be used for

✤ Data transferring

✤ Downloading

✤ Uploading
4
Data Task

✤ Task is the last object in the making request operation


chain, which contains the completion handler of the
response.

5
Response

✤ When making a request usually the response will be

✤ HTTP ERROR (400, 402, 403, 404, 500, 502, …etc)

✤ SUCCESS (200, 201, …etc)

6
Execution Steps

Main Thread

Normal app
execution
DataTask Thread Server
Session will open
new thread
Send request
and waits response

Dispatch and UI
process 7
Prepare Request

8
Prepare Request

✤ Create URL object from string

✤ it should be a valid URL


9
Prepare Request

✤ Create URLRequest object based on URL Object

10
Prepare Request

✤ Create URLSession Object using a default session


configurations

11
Prepare Request

✤ Create a DataTask that will send the request and


handle the response

12
Prepare Request

✤ Send the request

13
Prepare Request

✤ Any code will be written here, will be executed when


the response fetched from the server

14

You might also like