In a recent post I wrote about APIs and networking in iOS. In that discussion we learned how to anonymously access data. Today we will learn how to authenticate and access personalized user data. Before moving on, we need to look at the difference between authorization which means allowing someone to perform a certain action, and authentication which means validating someone’s identity. An example of authorization is the OAuth standard which facilitates logging in using third party services (e.g. log in with Twitter or Facebook). In order to authenticate, a user needs to first have an account. For this project you need to create an account for TheMovieDB API. Once the user logs in, a session has to be created so the user can interact with the API methods in order to get the desired information.
Let’s first create a Single View Application project in Xcode. In the storyboard add 2 text fields and set their placeholders to username and password respectively. Then add a button and name it Login. Finally, add a label under the login button so we can display debug messages when needed. Make sure to add all the necessary constraints. Then go to the view controller and add outlets for the text fields and label. Also create an action for the button:
The next step would be for us to create a session after logging in was successful. The API Sessions documentation tells us what the steps are:
Step 1: Create a new request token
Step 2: Ask the user for permission via the API
Step 3: Create a session ID
Step 4 (optional): Get the user id
Step 5 (optional): Display user information
For Step 1, let’s write a method named getRequestToken which constructs the necessary URL to get a token. We would then call this method inside the loginButton action method, right after the comment line:
// create a session here
self.getRequestToken()
We learned in the APIs and networking in iOS post how to make a network call using NSURLSession so let’s just repeat those steps. Let’s also add a few constants and variables we need:
Try logging in using dummy credentials and you should see a successful message printed on the label.
For Step 2, let’s log in using the token we got in the first step. Replace the successful block above with a call:
self.loginWithToken(self.requestToken!)
to a new method that we will create next:
This time you will have to use your real credentials to log in. If everything went right you should see a successful message telling you that you are now logged in.
For Step 3, we need to get a session ID. Replace the successful login block in the code above with a call:
self.getSessionID(self.requestToken!)
to a new method that we will create next:
If you log in again with your credentials, you should see the session ID printed on the label. A next logical step would be to get the user ID using the session ID we just got. Replace the successful block in the code above with a call:
self.getUserID(self.sessionID!)
to a new method that we will create next:
Run the app again and if everything went right, you should see the user id displayed. Now that we got everything we need, we can display personalized information about the user. Replace the successful block in the code above with a call:
self.completeLogin()
to a new method that we will create next:
Run the application again and you should see the first movie from your favorites list printed on the label. For simplicity, I went to my online profile page and favorited only one movie as you can see from the image below:
All the network calls we made so far are GET requests which means we have not written anything to our user account using the API. In order to be able to modify personalized information, such as adding or removing movies from the list of favorites, we need to make POST request using the same API.