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:

Then create an instance of service:

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

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

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

  1. Łukasz Pająk

Leave a Reply

Your email address will not be published.

white Land Rover
Business Featured Post

How BlaBlaCar beat the competition in the race for market domination in 27 countries.

When BlaBlaCar approached XSolve, they were at a stage of rapid growth with around 24 million users already buzzing about their excellent carpooling platform. But the visionaries at BlaBlaCar...

ebook cover
Business Featured Post

E-book: How to Create an Agile Office

We present to you an e-book which collects all our experiences and thoughts regarding creation of an agile office. Hands on, practical, functional. This resource was created primarily for...

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...

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