Book Image

Building Apple Watch Projects

By : Stuart Grimshaw
Book Image

Building Apple Watch Projects

By: Stuart Grimshaw

Overview of this book

With Apple’s eagerly anticipated entry into the wearable arena, the field is wide open for a new era of app development. The Apple Watch is one of the most important technologies of our time. This easy-to-understand book takes beginners on a delightful journey of discovering the features available to the developer, right up to the completion of medium-level projects ready for App Store submission. It provides the fastest way to develop real-world apps for the Apple Watch by teaching you the concepts of Watch UI, visual haptic and audio, message and data exchange between watch and phone, Web communication, and finally Visual, haptic as well as audio feedback for users. By the end of this book, you will have developed at least four fully functioning apps for deployment on watchOS 2.
Table of Contents (17 chapters)
Building Apple Watch Projects
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

WatchConnectivity


We have done this so often now, we can almost do it blind.

Create a new Swift file, making sure that the WatchKit target is selected, and name it WatchConnectivity.swift. Replace the import statement with the following code, most of which you have seen before:

Import WatchConnectivity

class WatchConnectivityManager: NSObject, WCSessionDelegate {

    static let sharedManager = WatchConnectivityManager()

    private override init() {
        super.init()
        if WCSession.isSupported() {
            let session = WCSession.defaultSession()
            session.delegate = self
            session.activateSession()
        }
    }

    func sendDataToWatch(data: AnyObject) {
        guard let contextData = data as? LocationSet else {return}

        let dataArray = contextDataFrom(locationSet: contextData)
        let context = [kApplicationContextDataKey: dataArray]
do {
            try WCSession.defaultSession().updateApplicationContext(context)
            print("updateApplicationContext...