Going the distance with the Active.com API
The following is a guest blog post from Sidney Maestre, a Platform Evangelist for StackMob, a leading provider of backend services for mobile developers. He has spent the last few years building mobile apps and sharing his knowledge with others. These efforts included speaking at Adobe MAX, 360iDev, SenchaCon, HTML5DevConf, Silicon Valley Code Camp, creating two courses, jQuery Mobile for Beginners and Learn Backbone.js + StackMob, for the uDemy.com platform and organizing the Bay Area Mobile meetup. Follow him on Twitter and Github at @SidneyAllen
Active.com, a Mashery API, provides an open data platform to help people gain access to activities anywhere. Active.com's API allows you to search for activities like triathlons, marathons and 5Ks as well as campground availability and golf tee times. Neil Mansilla, Platforms Evangelist at Mashery, created the Active.com demo app to show off their search API. Users simply enter a zip code and perform a search against the Active.com API. Results are displayed on a map.
Building on the Active.com demo, in this tutorial we're going to allow the user to save their favorite activities. But how to do this without spinning up servers and writing a lot of code? That's where MBaaS (mobile back-end as a service) comes in.
Most developers are familiar with PaaS (Platform as a Service) offered by companies like Heroku and AWS (Amazon Web Services). Instead of managing your own servers, you deploy your server code on their cloud infrastructure. MBaaS (mobile backend as a service), specializes in providing an API and SDKs for mobile developers to easily add backend services like data storage, social integration and push notifications without writing a lot of server-side code. StackMob is a leading MBaaS provider and a good fit for the new "favorites" feature.
StackMob Features Added to the Active.com App
We've added user management and authentication, data storage and access control levels, Users can click on an activity to see details. From the activity details screen you click "add to favorites" to save it. In order to associate favorites with a specific user, we've added a signup/login form. Users must be logged in to favorite an activity. Once logged in they can select the favorites tab to view all of their saved favorites. At any point, the user can logout of the app.
Creating a New App on StackMob
If you haven't already, take a minute to sign up for StackMob.
From the StackMob dashboard, click create new app. You'll give your application a name and select which platform you'd like instructions on how to get started. The getting started page shows you how to download and configure the StackMob SDK. You can skip this step if you are working with the Active.com demo.
For the Active.com demo code all you need is the StackMob public API key which you'll find under application information & keys and the Active.com API key. Open up the Active.com demo code and copy and paste your API keys inside the Constants.h file.
Storing Favorites on StackMob
Clicking on the disclosure arrow on the map will display the activity detail screen. Click the add to favorites button to save the activity to StackMob.
StackMob's iOS SDK uses Core Data to save and fetch data from StackMob. We've added the necessary frameworks and initialized StackMob in our AppDelegate. We've also created a User and a Favorite entity in our core data model.
Saving a favorite is done by creating a newManagedObject, setting the title and invoking the saveOnSuccess method which performs an asynchronous call to StackMob with a success and failure callback.
You'll notice if you try to save a favorite prior to logging in, you'll be redirected to the signup/login screen. From there you can create a new user profile or login with an existing one. You'll also notice that once you login the navigation bar will update with a logout button, so you can logout from any screen.
After you login, make sure you favorite at least one activity. The first time you save an object to StackMob, your schema will be auto generated for your app and populated with your object.
To confirm your data was saved, go to the StackMob data browser select the favorite schema and perform a query. You should see your favorite.
Adding Access Controls to Data
Now, that users are required to login, every favorite will have an "owner". Let's set the favorite schema permissions so a user must be logged in to create a new favorite and only the owner of the object can read, update or delete it.
Fetching Data from StackMob
If you return to the active.com demo app in the iOS simulator, you can retrieve your favorites by selecting the favorites tab. Any time you add another favorite, click the refresh button in the navigation bar to fetch the latest data from StackMob.
That concludes this tutorial, be sure to checkout the other services available through the StackMob Marketplace, and get ready for further integrations with Mashery's 50+ public APIs.Â