iOS

Our First WatchKit App – WatchCounter

In my last podcast episode, I talked about the Apple Watch Event and also a bit about what developing for the watch is like. WatchKit isn’t something that you can just create as a standalone application. Every WatchKit application must have a phone application. No processing happens on the watch, it is just a “dumb screen” for the application on the phone. So even if the entirety of what you are building is for the watch, the phone component must still be installed. The phone component is the “brains of the operation”.

Here is a diagram of WatchKit architecture from Apple
WatchKit Architecture from Apple, originally here: https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/Art/app_communication_2x.png

And, here is one that I created to expound on it a little bit:
WatchKit Architecture

Let’s walk through making a very simple application. The source for this application is available on GitHub, or you can follow along.

First, we need to make a phone application. You see that WatchKit isn’t any of our options, so I’m choosing to make a Single View application.
You cannot File->New WatchKit” title=”You cannot File->New WatchKit” /></p>
<p>Next, I name it WatchCounter, make sure the language is Swift and the device is the iPhone and click Next.<br />
<img decoding=

At this point, we just have a standard iOS application and XCode appears as it always does.
Xcode before adding WatchKit

With the project selected in the explorer pane, make sure that your Target window is visible. If not, click this button to show it.
Show Target Drawer in XCode

Next, click the plus sign indicated here to bring up the Add Target window.
Xcode Add Target

We are going to choose an Apple Watch -> WatchKit App Target Template
WatchKit Target

For the options, we are just making a simple application, so leave most of the defaults. We are going to make sure that the checkboxes are cleared next to “Include Notification Scene” and “Include Glance Screen”. If you forget, it won’t be the end of the world. Our Watch Storyboard will just be a little cluttered. Because we wouldn’t be using those screens for this sample, they wouldn’t affect what we’re doing right now.
Xcode WatchKit Target Options

After you are done and click “Finish”, you are presented with this dialog (if you haven’t previously told it to never show again). Click “Activate” so we can continue.
Activate WatchKit App scheme

At this point, your project will be changed. Added to our project are two new folders: WatchCounter WatchKit Extension, and WatchCounter WatchKit App. For this project, the only thing we care about in the “App” folder is the Interface.storyboard and the only thing in the Extension folder we need is InterfaceController.swift.
Our new project structure with the WatchKit pieces added

If we click the storyboard, we’ll see this:
WatchKit Storyboard

Go to the controls (bottom right of Xcode by default) and find the Button and Label controls.
Button and Label Controls in Xcode

Drag the label onto the watch storyboard, followed by the button. The watch has a “flow” layout and will just stack the controls on top of each other with no overlap. You don’t have access to auto-layout or precise positioning. There are ways to group some controls horizontally, but for now we can be fine with stacking the controls.
Button and Label on the Storyboard

Select the label on the storyboard and go over to the properties window and change the values to the ones that you see here. The label’s text will change when the app starts up, so I just have some text in there so it is easy to see and select while we are working with it visually.
Setting the label's properties

Next, select the button on the storyboard and then set its properties.
Setting the button's properties

Now, if you click the assistant editor button while on the storyboard, it will bring up the code for the view controller side by side.
Assistant Editor

Click on the label to select it and then hold down control, click on the label, and drag over to the code window and let go right above the awakeWithContext method. A dialog will come up to create an outlet. Name it displayLabel and click to accept.

Next, control click the button and drag over to the code window right below the awakeWitContext method. It will again ask you to create an IBOutlet, but use the dropdown to change to an IBAction, name it buttonWasPressed and accept.

Next, type in the rest of the code shown here. All we are doing is creating a counter variable and then updating the label, keeping track of how many times we clicked. It should be fairly straightforward.

import WatchKit
import Foundation


class InterfaceController: WKInterfaceController {
    @IBOutlet var displayLabel: WKInterfaceLabel!
    var counter = 0

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
        
        // Configure interface objects here.
        updateMessage()
    }

    @IBAction func buttonWasPressed() {
        counter++;
        updateMessage()
    }
    
    func updateMessage() {
        var message = "Pressed:\(counter) time(s)"
        displayLabel.setText(message)
    }
}

Now, make sure that the WatchCounter WatchKit App is still the active schema and you are targeting the iPhone 6 simulator.
Run the application

When you run it, should should launch the phone and watch simulator and get a watch app up that runs like this:
WatchCounter Demo

That’s it. If you have any questions, let me know. Again, if you want to check this out and poke around rather than follow along, the code for this application is available on GitHub.

Podcasts

Podcast Episode 35 – The Apple Watch

Apple WatchIn episode 35, I cover the Apple Watch Event. Of course, the two-hour event was not just about the watch, but that’s all most people cared about. I talk about what new things that we learned about the watch, some of the apps that are going to be available at launch, and what it is like to actually develop for the thing yourself. I also answer the most pressing question… will I buy one? Check it out and let me know what you think.


Links Mentioned in this Show:
Official Apple Watchkit Site
As I Learn WatchKit
WatchKit at RayWenderlich.com

You can also subscribe to the podcast at any of these places:
iTunes Link RSS Feed

Thanks to all the people who listen, and a special thanks to those who have rated me. I really appreciate it.

The episodes have been archived. Click Here to see the archive page.

Podcasts

Podcast Episode 34 – Conflicting Patterns, It Depends, and YAGNI

ConflictEpisode 34 is a little shorter than my normal solo podcasts, but it is a topic that I have been thinking about for awhile and I just wanted to get it off of my chest. Design Patterns aren’t like Duct Tape – the more you use, the better it is. In fact, they actually won’t all work together if you go in 100% on them. Today, I talk about 2 of those examples and then talk about how you should go about discovering good software design.


Links Mentioned in this Show:
Stack Overflow Question on how to write a library, giving possibilities across the DRY/Coupled spectrum
InfoQ – DRY Code and Coupling
TopTal Videos, the pick of the week

You can also subscribe to the podcast at any of these places:
iTunes Link RSS Feed

Thanks to all the people who listen, and a special thanks to those who have rated me. I really appreciate it.

The episodes have been archived. Click Here to see the archive page.

JavaScript

Podcast Episode 33 – Rob Eisenberg on Aurelia.js

Rob EisenbergLast Monday, Rob Eisenberg released Aurelia.js. This was a culmination of sorts of a whirlwind of activity that was kicked off when Rob left the Angular 2.0 team at Google. A lot of people wondered what he was up to (including me!), so I asked him to be on the show to talk about what’s been going on. We talk about SPAs, why he left Google, what’s special about Aurelia, how to handle learning all of these frameworks, and much more.


Links Mentioned in this Show:
Aurelia
Rob’s Leaving Angular Post
Rob’s Twitter
Rob’s Blog
Aurelia vs Angular Round One

You can also subscribe to the podcast at any of these places:
iTunes Link RSS Feed

Thanks to all the people who listen, and a special thanks to those who have rated me. I really appreciate it.

The episodes have been archived. Click Here to see the archive page.

Fluff

Skeet “Shooting”

Last year, at CodeMash v2.0.1.4, I really wanted to get a photo with Jon Skeet. Most people who know StackOverflow know that Jon Skeet has the highest reputation on the site by far. On top of that, he is an accomplished author, blogger, speaker, and all around nerd hero.

Jon has been to CodeMash for several years and I could never get up the nerve to interrupt him and ask for a picture. Finally, at last year’s event, I did it. My friend Russell and I got a picture with him… or so I thought. While I was able to take a picture of Russell with Jon, Russell failed to get a picture of me. In fact, he didn’t notice until we were walking away and I wanted to see the picture. No photo in the gallery.

Well, I wasn’t going to go back and bug him again. I felt like I already put him out. I did get to meet him and I had a pretty good story (of Russell’s failure), so I was ready to leave well enough alone. Poor Russell had to endure a year’s worth of teasing by co-workers for being unable to operate a camera.

Fast forward to CodeMash v2.0.1.5. Russell was going to go, but I decided to skip it this year. That meant that I wouldn’t be able to try for round 2 of my Jon Skeet photo shoot. We all decided, though, that it would be funny if Russell got a picture of Jon Skeet holding a picture of me. We were mostly kidding, but we found ourselves hilarious.

Russell was determined to redeem himself, so he reached out to Jon and set it up, tracked him down, and followed through. Somehow, out of the goodness of his heart, Josh allowed himself to be roped into the adventure of stalking Jon. Josh also made sure that a picture was actually taken this time. Russell then got the photo printed out and framed for my enjoyment. So I present to you my new favorite desk adornment:

Jon Skeet and I

This is all way better than if I had just gotten the picture last year! Big thanks to Russell and Josh!