Developing iOS Client App with Django rest framework

Hassan Abid
3 min readApr 16, 2016

--

Over the past couple of years, I have been using Parse as backend of my apps. However, it doesn’t give me a real feel of actually making an app from bottom up. I started using Django (A high-level Python web framework) two years ago but often lost interest due to its difficulty and completeness (less margin to learn basics). However, after trying some other popular backend languages such as PHP and NodeJS, I decided to stick with Django for a while.

Django’s main feature is its admin interface which helps developers to write apps as fast as they can! For over a year, I have used it to make few personal project sites including my portfolio. Due to my strong background in app development (iOS and android) I wanted to challenge myself by developing client apps with Django backend. Therefore, I started a small project for learning earlier this year. In this post I will explain how to setup Django rest framework and provide rest api end points for iOS Client app.

Django Restframework is modern Web Api took kit that supports Python (2.7 ~ 3.5) and Django (1.7 ~ 1.9).

For searchrestaurant app my goal was to provide following endpoints initially.

For this purpose I used Google Maps Geocoding for getting latitude and longitude of the searched place and Foursquare API for getting restaurants (pizza,coffee etc.) of the corresponding place.

The first step of creating a REST API with Django restframework is to declare a serializer class of the model. The purpose of serializer and de-serializer is to get retrieve/send data in form of json. I created RestaurantSerializer in a file serializers.py under the app directory search.

Next step to use this serializer and serve json data when api endpoint is hit. All of the api end points are handled by RestaurantList class in views.py using url(r’^api/v1/$’,views.RestaurantList.as_view()) in urls.py

First of all, the get request is parsed for parameters such as location and rtype (restaurant type) and then queried the existing database. In case, data doesn’t exist, a new set of queries is generated using both Google API followed by Foursquare API. The data returned by Foursquare is saved in database and returned to api endpoint using serializers class. Code snippet is provided as below.

A typical request and response object will look like below.

Request : https://searchrestaurant.pythonanywhere.com/api/v1/?format=json&location=oslo&rtype=pizza

Response :

Please note that we get JSON Array as response. In case no data is found we got response with following JSON Object

{“error”:”not-found”,”status”:”404"}

From here on, I will explain iOS Code to retrieve the restaurant list and show in the app. I used Alamofire for get request (It is popular networking library for Swift same as AFNetworking)

For better handling of JSON we can use SwiftJSON Library. The data retrieved using the API is saved and later provided to TableViewController.

Search Restaurant (Using Google Place Picker you can select current location too)
Restaurant List sorted based on number of checkins

While this is the very first version of a simple client app with Django restframework, I am writing some of the missing elements which I plan to implement for next version

  • API response should return a JSON Object with a status code (200, 400 etc.) instead of JSON Array
  • There should be a mechanism for authentication (or limit API usage)
  • Major uplift in iOS UI required -:)

I wrote part 2 this post for Android. Check out here

--

--