Episode #200

Building a WatchKit App

18 minutes
Published on December 10, 2015

This video is only available to subscribers. Get access to this video and 584 others.

In this episode we are joined by Conrad Stoll who shows us how to build a watchOS 2 app to order beer. Data is synchronized from an iOS app, and the digital crown is used to make selections.

About the Guest Screencaster

Conrad Stoll is the author of MMWormhole, a useful library to facilitate message passing between iOS apps and extensions. He's also an iOS Architect at Mutual Mobile. You can find him on Twitter or on his blog.

Episode Links

Using WCSession to Send Data

import WatchConnectivity

let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()

var context = session.applicationContext
context["key"] = value

do {
    try session.updateApplicationContext(context)
} catch let error {
    print(error)
}

Using WCSessionDelegate to Receive Data

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
    if let arrayOfDictionaries = applicationContext["key"] as? [[String : AnyObject]] {

    }
}

Updating WKInterfacePicker

@IBOutlet weak var picker : WKInterfacePicker?

var items : [WKPickerItem] = []

for beer in beers {
    let item = WKPickerItem()
    item.title = beer.title

    if let image = beer.image {
        item.contentImage = WKImage(image: image)
    }

    items.append(item)
}

self.picker?.setItems(items)

Responding to Picker Changes

@IBAction func pickerDidChange(index: Int) {
    let beer = beers[index]
    currentBeer = beer
}

This episode uses Watchos 2.