Adding support for a REST API can give to your application a great appeal, since it shall be able to connect to remote web services (through the rest API) that can enhance the features of the application itself.
In this simple tutorial we are going to prepare a simple REST API using Slim Framework, and then we are going to make requests to the API using AlamoFire in swift.
The REST API will offer two requests:
- get the value of an item (GET)
- store a new value for an item using simple Authorization header (POST)
NOTE: this tutorial is not a step by step tutorial, you should be familiar with the following items:
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
Alamofire is an HTTP networking library written in Swift.
First of all we have to install the web server and the PHP language, you can find a package for your system that will install all that you need. Take a look at LAMP, WAMP, XAMP.
Open a terminal window and move to the httpdocs directory
$ cd /path/to/httpdocs
(on Linux you should move into /var/www)
Then we have to install composer (http://getcomposer.org).
Then create the Slim skeleton using composer:
Edit the index.php inside the directory restapi/public/
Before the line containing “$app->run();” insert the following code:
The first function implements a GET request on the route http://127.0.0.1/restapi/public/item/, the request accepts a parameter in plain form and returns the parameter and a new value (parameter +100) in json format.
The second function implements a POST request on the route http://127.0.0.1/restapi/public/item/, the request accepts a parameter in json format and the Authorization code through the HTTP headers and returns back the input parameter with an error field set to false if the Authorization code is correct and true if it is not correct.
Open your favourite browser and goto https://github.com/Alamofire/Alamofire, click on the “Download Zip” button and wait for the completion of the download. Extract the zipped archive where you prefer.
Creating the swift project.
Open XCode and create a new Mac OS X Cocoa project, the language must be “swift”.
To install the downloaded AlamoFire into your project, follow the instructions at https://github.com/Alamofire/Alamofire for manual installation.
Modify the viewDidLoad method as follows:
Now run your application. In the output you should see something like the following lines
Some explanations about the swift code could be useful…..
To make a request with Alamofire you have to call the request method. Alamofire handles all the methods defined in RFC 7231 §4.3:
OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE, TRACE, CONNECT
The first bunch of code makes a GET request.
Alamofire asynchronously waits for the response (see .responseJSON), as soon as the response will arrive it will be managed by the .responseJSON handler.
The second request is more complex, since it sends the post parameters as HTTP body in json format and it also sends the HTTP headers containing the authorisation code.
To sends parameters and headers you have to prepare the two dictionaries defined above, then you have to pass them to the Alamofire.request method
the .responseJSON handler is the same of the first request.
In this short tutorial we printed out the response, if you want to manage the response in son format SwiftyJSON can help you….
But this is another story.