Networking Concepts Flashcards

iOS

1
Q

iOS Networking:

What are the main components of a HTTP URL? What purpose do they have?

A

Every HTTP URL consists of the following components:

scheme: //host: port/path?query
1. Scheme - The scheme identifies the protocol used to access a resource, e.g. http or https.
2. Host - The host name identifies the host that holds the resource.
3. Port - Host names can optionally be followed by a port number to specify what service is being requested.
4. Path - The path identifies a specific resource the client wants to access.
5. Query - The path can optionally by followed by a query to specify more details about the resource the client wants to access.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

iOS Networking:

Give some examples of HTTP request methods you know. What purpose do they have?

A

HTTP defines a set of request methods to indicate the desired action to be performed for a given resource. Examples are:

  1. GET - retrieve a resource
  2. POST - create a resource
  3. PUT - update a resource
  4. DELETE - delete a resource

Other HTTP request methods are HEAD, OPTIONS, PATCH etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

iOS Networking:

How would you create a HTTP request in iOS?

A

One way of creating a HTTP request in iOS is by using URLRequest with URLComponents.

var components = URLComponents()
components.scheme = "https"
components.host = "api.github.com"
components.path = "/search/repositories"
components.queryItems = [
    URLQueryItem(name: "q", value: "swift"),
    URLQueryItem(name: "sort", value: "stars")
]

let url = components.url

With URLComponents you can easily define the URL components and then create a URLRequest out of it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

iOS Networking:

How would you send and receive HTTP requests in iOS?

A

One possibility for sending and receiving HTTP requests is by using URLSession. With URLSessionConfiguration, you can configure caching behaviour, timeout values and HTTP headers.

A session works with tasks. After creating a session you can use URLSessionDataTask to send a request and to get the response:

let urlSession = URLSession(configuration: .default)
let task = urlSession.dataTask(with: url) { data, response, error in
    // Handle response.
}
task.resume()

You can also use URLSessionDownloadTask to download and URLSessionUploadTask to upload files. You can suspend, resume and cancel tasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

iOS Networking:

The server response contains a HTTP status code. Give some examples and explain their meaning.

A

The HTTP response status codes are separated into five categories:

  1. 1xx Informational: Request was received, process is continuing.
  2. 2xx Successful: Request was successfully received, understood and accepted.
  3. 3xx Redirection: Further action needs to be taken to complete the request.
  4. 4xx Client Error: Request contains bad syntax or cannot be fulfilled.
  5. 5xx Server Error: Server failed to fulfill an apparently valid request.

Examples:

  • 200 OK: Request was successful.
  • 201 Created: Request was successful, a new resource was created.
  • 400 Bad Request: Server did not understand the request.
  • 401 Unauthorized: Authentication has failed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

iOS Networking:

In iOS, a networking feature called App Transport Security (ATS) requires that all HTTP connections use HTTPS. Why?

A

HTTPS (Hypertext Transfer Protocol Secure) is an extension of HTTP. It is used for secure computer network communication. In HTTPS, the communication protocol is encrypted using TLS (Transport Layer Security).

The goal of HTTPS is to protect the privacy and integrity of the exchanged data against eaves-droppers and man-in-the-middle attacks. It is achieved through bidirectional encryption of communications between a client and a server.

So through ATS, Apple is trying to ensure privacy and data integrity for all apps. There is a way to circumvent these protections by setting NSAllowsArbitraryLoads to true in the app’s Information Property List file. But Apple will reject apps who use this flag without a specific reason.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

iOS Networking:

RESTful APIs typically return the response data in JSON format. What value types are supported by a JSON schema?

A

JSON supports four basic value types and two complex data types.

The basic types are string, number, boolean and null.

The complex data types are object and array. An object is an unordered set of name/value pairs. An array is an ordered collection of values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

iOS Networking:

How would you convert a JSON response into native Swift types?

A

Swift’s Codable protocol makes it easy to convert JSON to native Swift structs and classes. The first step is to define a Swift type that has the same keys and value types as the JSON and conforms to Codable. For example:

struct Doggy: Codable {
let name: String
let age: Int
}

Then we can use JSONDecoder to convert:

let decoder = JSONDecoder()
do {
    let doggies = try decoder.decode([Doggy].self, from: jsonData)
    print(people)
} catch {
    print(error.localizedDescription)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

iOS Networking:

What is the basic idea behind the OAuth protocol?

A

OAuth lets users grant third-party services access to their web resources, without sharing their passwords. This is possible though a security object known as access token.

If a third-party app wants to connect to the main service, it must get its own access token. The users password is kept safe inside the main service and cannot be obtained from the access token. Access tokens can then be revoked if the user does not want to use the third-party app anymore.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

iOS Networking:

Do you have experience with third-party iOS networking libraries? If so, which ones?

A

The interviewed person could name following libraries:

Alamofire - an abstraction layer over URLSession to make networking more simple and elegant

Moya - an abstraction layer over Alamofire that creates a type-safe structure for network services and requests

How well did you know this?
1
Not at all
2
3
4
5
Perfectly