2. Swift API Installation

Here is a tutorial to install Swift on MAC and get access to the Swift API for your MetaSensors.

You should have completed the instructions in the previous section related to the Xcode Installation.

We will be using XCode so you should be comfortable with this IDE.

Swift compilers are already installed with XCode and the Swift language can be interpreted by the XCode IDE.

2.1. Install MetaWear

MetaWear is available as a CocoaPod to be added to your Xcode project. This is the recommended way to add MetaWear to your own iOS App if you already know how to use CocoaPods.

If you are a first time CocoaPods user, please see this tutorial.

If you are not comfortable with CoacoaPods, we recommend using our StarterApp project where everything has already been setup for you including a Podfile. Simply jump to to the next section.

To install it, simply add the following line to your Podfile:

>>> pod "MetaWear", :subspecs => ['UI', 'AsyncUtils', 'Mocks', 'DFU']

Save your Podfile.


>>> pod install

Open the MyApp.xcworkspace that was created.

2.2. Usage

Import the framework header files like this:

>>> import MetaWear
>>> import MetaWearCpp

2.3. Download API Repository

Head over to our Swift Github page: https://github.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS

You can clone the repository or simply download as a ZIP file:

>>>  git clone https://github.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS.git

Make sure the MetaWear-SDK-Cpp submodule is also downloaded. If it is not present, use the command:

>>>  git clone --recurse-submodules https://github.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS.git

2.4. About Swift APIs

The Swift APIs are a thin wrapper around the MetaWear C++ API so you will find the C++ documentation and API reference useful.

When you download our Repository, you will find:

  1. MetaWear -> Containing the Swift wrappers and the C++ MetaWear library Repository (this is what we submit to Cocoapods and includes all the source code)

  2. Starter Project -> An skeleton App for you to get started with (with the MetaWear Cocoapods pre-loaded)

  3. MetaWear API Test -> An example App for you to experiment with (with the MetaWear Cocoapods pre-loaded)

  4. Docs -> Documentation

  5. Images

We also have Swift API documentation here. Please keep this documentation handy.

2.5. Template App

For those who want to build a simple MetaWear App or are new to iOS development, we have provided a template for creating MetaWear Apps.

Go to the MetaWear-SDK-iOS-macOS-tvOS/StarterProject directory.


>>> pod update

Open StarterProject.xcworkspace with Xcode.


Let’s build our first App!

Make sure you are confident with Xcode and Swift, there is a great tutorial here.

2.5.1. UI Elements

To add a start and stop button using interface builder, so open up iOS/Main.storyboard.

Then drag two buttons onto the Device View Controller and connect the buttons to DeviceViewController.swift.


2.5.2. Data Stream

Switching back to the DeviceViewController class, lets fill in the startPressed and stopPressed functions.

The start button will enable the accelerometer data stream. The stop button will disable the stream.

@IBAction func startPressed(sender: AnyObject) {
    let board = device.board
    guard mbl_mw_metawearboard_lookup_module(board, MBL_MW_MODULE_ACCELEROMETER) != MODULE_TYPE_NA else {
        print("No accelerometer")
    let signal = mbl_mw_acc_get_acceleration_data_signal(board)
    mbl_mw_datasignal_subscribe(signal, bridge(obj: self)) { (context, data) in
        let _self: DeviceViewController = bridge(ptr: context!)
        let obj: MblMwCartesianFloat = data!.pointee.valueAs()

@IBAction func stopPressed(sender: AnyObject) {
    let board = device.board
    let signal = mbl_mw_acc_get_acceleration_data_signal(board)

2.5.3. Test the App

Connect your iOS device to your Mac.


Load the StartApp onto your iOS device and connect to your board.


Press the START button to see acceleration data being outputted to the log.


2.6. Sample App

For those who want to see coded examples of the functionalities of the Metawear, we have provided a MetaWear API test App.

Go to the MetaWear-SDK-iOS-macOS-tvOS/MetaWearApiTest directory.


>>> pod update

Open MetaWearApiTest.xcworkspace with Xcode.


This sample MetaWearAPI Test App shows you how to update firmware, graph accelerometer data, blink the leds red and much more. It is best for you to open up the project, run it on your phone and read through the code.


Please note this is only an example App. It doesn’t support automatic reconnects, long term logging, or any functionalities you should have in a public published App.

When are you want learn more about the inner workings of the APIs and advanced functionalities of the MetaWear, go to the Next Steps.

2.7. Next Steps

Go over to our Developers Section to learn more about the Swift development.