2. Swift

2.1. Swift

This tutorial will take you through your first Swift App.

2.1.1. Prerequisites

Before we get started with code examples, you should be familiar with Swift and creating iOS apps. If you are new to iOS development, download Xcode and use the new project template to build a Hello World app for your iOS device.

You should already have followed the instructions on the Apple Installation page of our tutorial.


You can also build your apps in Objective-C, but it’s highly recommended to begin all new development in Swift.

2.1.2. 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 that we will be using in this guide. Download the template open StarterProject/StarterProject.xcworkspace with Xcode.

Advanced developers building more complex apps can refer to the iOS Documentation for explicit installation instructions of the MetaWear SDK.

Clone the StarterProject App Template:

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

Alternatively you can also download as a ZIP file from the site: https://github.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS 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.

See Apple Docs for detailed instruction on this. 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) != MBL_MW_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)
} Test the App

Load the app onto your iOS device and connect to your board. Press the start button to see acceleration data being outputted to the log.

2.1.3. Sample App

We have created an iOS App for you that runs on an iPhone and includes many examples from graphing sensor data live to turning on and off GPIOS:

Clone the MetaWear-SampleApp-iOS App from Github:

>>> git clone https://github.com/mbientlab/MetaWear-SampleApp-iOS

ALternatively you can also download as a ZIP file from the site: https://github.com/mbientlab/MetaWear-SampleApp-iOS

Open the MetaWearApiTest.xcworkspace file with Xcode.

Make sure you have an iPhone plugged in to your Mac and run it.

_images/apple-xcode-1.jpg Next Steps

For advanced developers wanting a deeper dive into the iOS SDK, checkout the documentation page for links to additional information about the SDK.