iOS Interview Questions and Answers for Senior Developers Part 2 – Networking

This post covers questions and answers on the networking topic in iOS.

The purpose of these questions is to get to know the developers general knowledge of computer networking concepts. And also to check his specific knowledge on networking in iOS.

I recently got into the position of leading the technical interview when my client was searching for a new senior iOS developer. It’s a challenging task to evaluate the skills and knowledge of another developer. So in this post, I want to share my results of the most useful iOS and Swift questions and answers with you. I categorised the questions into different topics:

  1. Swift Programming Language (covered in part 1)
  2. Networking (covered in this post)
  3. Persistence / Databases (coming soon)
  4. Concurrency (coming soon)
  5. Architecture, Design Patterns & Clean Code (coming soon)
  6. Testing (coming soon)
  7. Continuous Integration (coming soon)

Although you can perform some high-level networking tasks without learning about the underlying protocols, knowing how things work at a low level will help you better understand why things go wrong and how to handle them. So if you want to dive deeper into networking concepts, here is a list of books available on Amazon I can recommend:

So let’s get started with the interview questions and answers.

Hey buddy, let’s find out how to do some networking.

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

Every HTTP URL consists of the following components:

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

2. Name some of the HTTP request methods you know. What purpose do they have?

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

  • GET – retrieve a resource
  • POST – create a resource
  • PUT – update a resource
  • DELETE – delete a resource

Other HTTP request methods are HEAD, OPTIONS, PATCH etc.

3. How would you create a HTTP request in iOS?

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.

4. How would you send and receive HTTP requests in iOS?

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.

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

The HTTP response status codes are separated into five categories:

  • 1xx Informational: Request was received, process is continuing.
  • 2xx Successful: Request was successfully received, understood and accepted.
  • 3xx Redirection: Further action needs to be taken to complete the request.
  • 4xx Client Error: Request contains bad syntax or cannot be fulfilled.
  • 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.

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

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.

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

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.

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

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(doggies)
} catch {
    print(error.localizedDescription)
}

9. What is the basic idea behind the OAuth protocol?

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.

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

The interviewed person could name following libraries:

  • Alamofire – an abstraction layer over URLSession to make networking more simple and elegant
  • AlamofireImage – an image component library for Alamofire
  • Moya – an abstraction layer over Alamofire that creates a type-safe structure for network services and requests

Of course, there are a lot more iOS networking libraries. An extensive list of available libraries maintained by Vinicius Souza is available here.


Did you like this post? Then let me know by clicking on the like button below.

Like to stay updated? You can follow me on Twitter or subscribe to this blog. If you like to read on Medium, I’m also publishing my posts there. Feel free to contact me if you have any questions or want to give me some feedback.

4+