Last year, gap intelligence started looking into developing mobile applications for our external customers. While we already have a native iOS app, we selected RubyMotion as the framework for developing these new mobile apps. We're primarily a Ruby shop, so we thought this would minimize the learning curve and streamline development across both iOS and Android.
Our mobile apps need to authenticate users and connect to our existing API. I couldn’t find a complete example of this so I combined different sources to come up with a solution that works for us. Below is the step-by-step process. This assumes you have created a RubyMotion project and have done a tutorial or two to get familiar with the framework. The source code, references, and tutorials are listed at the end. Enjoy.
First, we'll add some gems to our Gemfile. We'll use afmotion for sending http requests and bubble-wrap to easily parse the JSON responses. After adding, add
require 'bubble-wrap' to your
Rakefile. Then run
The first major class we'll look at is our
AppDelegate. This is mostly auto-generated by RubyMotion, but we will add the code to initialize and display our
SessionsController, which will handle our login.
The View Controller
Now we'll start building out our controller, which will inherit from
UIViewController. We'll build our login form in the
viewDidLoad method (which you've probably seen in RubyMotion tutorials).
Building the View
You can see that instead of including view logic in the controller, we'll extract this into a
This class builds the form components, adds them to the view and makes them accessible for the controller. This could probably be refactored or take advantage of a form building gem if it became more complex. Here is what the form looks like:
Talking to the API
Before going back to our controller, we'll build out two other objects that our controller will need. The first is
ApiClient. This client will handle communication between our app and the API. This is where we'll use the
AFMotion client to make requests. In this example, our API is using username/password authentication through OAuth and will return an access token upon successful login. Realistically, we would probably add methods for any additional endpoints we need to hit on our API after a user is authenticated. For now it's pretty simple:
Storing User Info
The other object our controller will need is a
user model so we can keep track of the user's information and the API token to use in future requests. We'll utilize the app's Persistence store to securely save this data.
Connecting All the Pieces
Looking back at our controller, we're now ready to tie it all together. We've added a target to the
login_button. The target, also called
login, is the name of the method that will be called with the login button is tapped. Let's look at that method along with three helper methods that it will use.
The login method will use the api_client to make the request, drawing the username and password from the form view. Once it gets a response it will save the user info and display a welcome message or an error.
And that's it. The complete source code can be found here: https://github.com/GapIntelligence/api-auth-sample-ios
Questions and feedback are welcome in the comments or directly at email@example.com. We're always interested in other solutions as we get deeper into working with RubyMotion. Thanks!