The LoopBack iOS SDK eliminates the need to use the clunky
NSURLRequest and similar interfaces to interact with a LoopBack-based backend. Instead, interact with your models and data sources in a comfortable, first-class, native manner.
- Knowledge of Objective-C and iOS App Development
- Mac OSX with Xcode 4.6 or higher
- For on-device testing, an iOS device with iOS 5 or higher
- A LoopBack-powered server application.
- App schema. Explaining the type of data to store and why it is outside the scope of this guide, being tightly coupled to your application's needs.
The LoopBack iOS guide app
The easiest way to get started with the LoopBack iOS SDK is with the LoopBack iOS guide app. The guide app comes ready to compile with XCode, and each tab in the app guides you through the features available to mobile apps through the SDK. Here are some representative screenshots:
From your usual projects directory:
Download the LoopBack guide application to your local machine from GitHub:
Open the Xcode project downloaded as a part of the Guide Application's Git repository.
Run the application from Xcode (Command+R by default) and follow the instructions on each tab. Popup dialogs in the application will ask you to uncomment various code blocks in each ViewController illustrating how to use the LoopBack SDK to interact with models stored on the server.
Getting Started with the iOS SDK
If you are creating a new iOS application or want to integrate an existing application with LoopBack, use the LoopBack SDK directly (LoopBack.framework), independent of the guide application.
Follow these steps:
Open the Xcode project you want to use with LoopBack, or create a new one.
Drag the entire LoopBack.framework folder from the new Finder window into your Xcode project.
Important:Make sure to select "Copy items into destination group's folder". This places a copy of the SDK within your application's project folder.
Verify LoopBack is included in the list of iOS Frameworks to link against your binary. In your Project settings, check the 'Link with Binaries' section under the 'Build Phases' tab. If it's missing, add it directly by clicking the '+' button and selecting LoopBack.framework.
Import the LoopBack.h header into your application just as you would Foundation/Foundation.h. Type this line:
You need an
Adapterto tell the SDK where to find the server. Enter this code:
LBRESTAdapterprovides the starting point for all our interactions with the running and anxiously waiting server.
Once we have access to
adapter(for the sake of example, we'll assume the Adapter is available through our AppDelegate), we can create basic
LBModelRepositoryobjects. Assuming we've previously created a model named "product":
All the normal
LBModelRepositorymethods (for example,
findById) are now available through
Go forth and develop! Check out the API docs or create more Models with the slc command-line tool.
Creating a sub-class of LBModel
Creating a subclass of LBModel enables you to get the benefits of an Objective-C class (for example, compile-time type checking).
Model interface and properties
As with any Objective-C class, the first step is to build your interface. If we leave any custom behavior for later, then it's just a few
@property declarations and we're ready for the implementation.
Since we've left custom behavior for later, just leave this here.
Because of this the repository class name (
'widget', above) needs to match the name that model was given on the server.
Use this to make creating Models easier. Match the name or create your own.
LBModelRepository provides a basic implementation, we only need to override its constructor to provide the appropriate name.
Remember to use the right name:
A little glue
Just as you did in Getting started, you'll need an
LBRESTAdapter instance to connect to our server:
" with the complete URL to your server. "
Once you have that adapter, you can create a repository instance.
Using the repository instance
Now that you have a
WidgetRepository instance, you can create, save, find, and delete widgets, as illustrated below.
Create a Widget:
Save a Widget:
Find another Widget:
Remove a Widget: