Nowadays, almost every mobile application connects to a server in order to exchange data. This is a key consideration at the beginning of any project: the developer must decide whether to write a native manager, which app will use to connect with API or opt for one of the heavy libraries and use a small percentage of its capabilities. We decided to create our own library and after over two months of work, finally, we can introduce SwiftAPI, our first iOS library.

You may ask: Why create another iOS library if there are already plenty of similar ones? That’s very good question. The only real justification for creating something new is because it is needed, and that is exactly the case here.

We required the library to be:

  • written in the latest version of Swift,
  • easy to upgrade,
  • as light as possible,
  • ready to use out of the box,
  • able to handle the most commonly used network operations.

After over two months of work, finally, we can introduce SwiftAPI, our first iOS library available at

How it’s built

SwiftAPI is built using Swift 3, dedicated to iOS, OS X, tvOS and watchOS. The library is split into three layers.

  1. The first implements creating requests for sending data, uploading and downloading files. It also allows users to send, suspend, resume and cancel multiple requests. Running requests in the background is also supported. For network tasks, URLSession class from the Foundation framework is used. The whole layer is internal and not available for use outside the library.
  2. The second introduces the public, easy to use ApiService, helper classes, and structures. This allows the sending of predefined requests to a URL with parameters. Any parameters not required are set to default values, allowing you to customize only those parameters you need. Consequently, the code you are writing is much clearer and more readable. The methods for sending requests are split into two parts: the first is for data requests, and the second is for files. The ApiRequest structure allows you to manage each particular request or simply follow its progress. For every request you can add an array of custom headers, decide if it should be sent in the foreground or background, and whether you would like to follow its progress. In cases of a positive server response, the completion handler will contain an ApiResponse instance, which includes response status code, headers, and data. However, if a request fails, the completion handler will give an Error instance, describing the problem.
  3. The third layer adds the functionality of the REST service, helper protocols, and structures. The RestService consists of the base URL of server and API path that may be used for switching between different API versions. It also allows GET, POST, PUT, PATCH or DELETE commands to be applied to data or file resources. Every method of service returns an instance of ApiRequest to allow management of the request. Similar to the second layer, any unrequired parameters are set to default values.

How to use it

Let’s assume that we want to download an image using our library. As an example, we’ll use the Wikipedia logo available here: logo. For this purpose, ApiService will be perfect.

First, let’s import our library:

import SwiftAPI

Then create an instance of service:

let service = ApiService()

Now, let’s define the source and destination URLs:

let sourceUrl = URL(string: "")!
let destinationUrl = Bundle.main.url(forResource: "wikipedia", withExtension: "png")!

As a destination, we use the main bundle folder.
The only thing that remains is downloading:

_ = service.downloadFile(from: sourceUrl, to: destinationUrl) { (response, error) in
  if let error = error {
    print("File could not be downloaded, because: \(error.localizedDescription)")
  } else if let response = response {
    print("Downloading finished with status: \(response.statusCode.rawValue) \(response.statusCode.description)")

And that’s it! If you have any questions or feedback just get in touch with me.

Holacracy why it’s a good idea to share the power
Business Featured Post

Holacracy: why it’s a good idea to share the power

We are on standing on the brink of the AI revolution. Researchers at the University of Oxford predict that in the next two decades up to 66% of American...

problems of software outsourcing
Business Featured Post

5 Problems of Software Outsourcing: a briefing for the decision-maker

The fourth article in our “CTO asks” series, addressing real issues, which CTO’s need to tackle in their daily work. This question was asked by Gianluca Bisceglie from Visyond....

What makes a great product owner XS blog
Business Featured Post

What makes a great Product Owner? A story behind iPhone’s success

This is the third article in our series “CTO asks” addressing real issues, which CTO’s need to tackle in their work. This question was asked by Cornel Studach from...

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies.

To find out more click here

Ok, Thanks