Skip to content

Using LoopBack iOS SDK with Cocoapods in Swift

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.
    }


}

 

Published inProgramming

6 Comments

  1. hideya

    Thanks for the great doc, Kevin!
    One question — haven’t you used “use_frameworks!” in the Podfile? With it, you can just say “import LoopBack” in your swift file just like when using an ordinary module instead of being bothered by the bridging header business.

    • kgoedecke

      Hey Hideya,
      thanks for your feedback I really appreciate it! Your suggestion makes a lot more sense, I’ll upload a revised version within the next few days.

      Cheers

      Kevin

  2. Len

    Hey there!
    I have followed every single step of implementing your version of LoopBack iOS SDK with Cocoapods. Sadly, I am stuck having not able to find Objective-C Bridging Header in my build settings were you ask to insert the path to the file that I just created. I am unable to locate the Objective-C Bridging Header in my xcworkspace!

    Please help me out here!! Thanks and much appreciated.

    • kgoedecke

      I updated the tutorial, it’s not necessary anymore to have the bridging header.
      Can you please go through the tutorial again and see if that resolves your issue?
      To sum it up simply add “use_frameworks!” to your Podfile and run “pod update” in your project dir, this should do the trick.

  3. Phone Z Phyo

    Anything related to Loopback doesn’t come with good examples and documentations. This is an exception. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *