WatchKit: Open Your iOS App From The Watch

UPDATE: It is NOT currently possible to open your iOS app from the Apple Watch. This was a bug with that version of Xcode, which has since been fixed. See comments for more information or a short explanation of my mistake.

The most exciting part of yesterday’s Xcode 6.2 Beta 2 release was the new added ability to open the iOS app from the Apple Watch and even pass information while doing so! It’s super easy to do.

Here is the simple sample app I built:

As the user clicks on a button in the Watch App, it triggers the opening of the iOS app. Here is how to make this app:

The Setup

Make sure to download XCode 6.2 Beta 2 or up for this to work! If you haven’t gone through the process of making a Hello, World WatchKit app, follow the instructions outlined in my How To Create A “Hello World” WatchKit App post.

The Design

In Interface.storyboard, drag buttons onto the InterfaceController, and change the Button Title Colors as below:

Screen Shot 2014-12-12 at 5.10.34 AM

Create IBActions

For each button, create an IBAction by Control + Dragging each button to the Interface Controller:

Insert_Connection_and_Interface_storyboard_—_Edited

Now comes the fun part!

Open Application

We’re going to use the new openParentApplication:reply: class method on the WKInterfaceController to open our application. Here is the Documentation for it:

Screen Shot 2014-12-12 at 5.23.42 AM

Since all the IBActions will open up the parent app, but send a different color, I created a function they can all call for more DRY code:

InterfaceController_swift_—_Edited

Run your Watch app! Now, when you click on the buttons, it’ll open up your parent iOS app. Of course, the last part is to use the information passed to the parent iOS app to do something (like change background color in this case)…

Handle WatchKit Extension Request

There is now a new method for the App Delegate to handle the WatchKit Extension Request:

AppDelegate_swift

Since we have to handle the WatchKit request in the AppDelegate, my implementation includes just posting a notification, so that any object that depends on the information in the WatchKit request can just handle it as needed.

Now, the iOS app’s ViewController can listen for the notification and change color accordingly:

//  ViewController.swift

import UIKit

class ViewController: UIViewController, UIAlertViewDelegate {

    private enum Color: String {
        case Red = "red"
        case Green = "green"
        case Orange = "orange"
        case Blue = "blue"

        func color() -> UIColor {
            switch self {
            case Red:
                return UIColor.redColor()
            case .Green:
                return UIColor.greenColor()
            case .Orange:
                return UIColor.orangeColor()
            case .Blue:
                return UIColor.blueColor()
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: Selector("handleWatchKitNotification:"),
            name: "WatchKitSaysHello",
            object: nil)

    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func handleWatchKitNotification(notification: NSNotification) {
        if let userInfo = notification.object as? [String : String] {
            if let colorStr = userInfo["color"] {
                let color = Color(rawValue: colorStr)
                view.backgroundColor = color?.color()
            }
        }
    }

}

That’s it! This is all pretty simple, but think of all the possible interactions this new WatchKit capability allows! I can see lots of fun games taking advantage of this… And this is all only in Beta 2. A lot more to come for sure!

You can view the source code on Github here.

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.