The LoopBack Framework provides an easy way to create RESTful APIs and also comes with SDKs for iOS and Android, which makes it really convenient to interact with the Backend from your apps.

The iOS SDK is available on the official website (LoopBack Docs) and on GitHub (Loopback iOS SDK). I noticed that the version in the docs isn’t up-to-date, that’s why I decided to use the latest version of the SDK from GitHub. The developers provide a podspec file in the repo so you can easily import the Framework in your project without compiling it every time.

Prerequisites

  • An existing XCode project using Swift

Let’s get started

Step 1: Install Cocoapods

You can skip this step if you already have cocoapods installed on your Mac. Otherwise simply open your terminal and run the following command:

sudo gem install cocoapods

That’s it, you’re now ready to use cocoapods!

Step 2: Create a Podfile

Close your XCode project and in the main folder create a new file named “Podfile”.

Insert the follwing:

pod 'LoopBack', :git => 'https://github.com/strongloop/loopback-sdk-ios.git'

use_frameworks!

Step 3: Install the Pods

Install the Cocoapods running the following command in your project directory:

Kevins-MacBook-Pro:loopback-swift-example kevin$ pod install

Cocoapods will now look for the podspec file in the GitHub repo and download the necessary framework.

You should see the following ouput:

Kevins-MacBook-Pro:loopback-swift-example kevin$ pod install
Updating local specs repositories
Analyzing dependencies
Pre-downloading: `LoopBack` from `https://github.com/strongloop/loopback-sdk-ios.git`
Downloading dependencies
Installing LoopBack (1.3.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `loopback-swift-example.xcworkspace` for this project from now on.
Sending stats
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod
installed.

Step 4: Usage

From now on you’ll use the xcworkspace file in your project directory every time you open your app.

Go ahead and open the xcworkspace file to fire up XCode.

You’re now ready to use the SDK in your Swift project.

In the swift file simply use import to include the LoopBack SDK and you’re good to go!

Example Usage:

//
//  WidgetViewController
//  loopback-swift-example
//
//  Created by Kevin Goedecke on 3/19/16.
//  Copyright © 2016 kevingoedecke. All rights reserved.
//

import UIKit
import LoopBack

class WidgetViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}