WatchConnectivity gotcha. willActivate()

This may seem simple for many who have been busy developing Watch extensions for the Apple Watch, but it stumped me for about a minute. Not long, but long enough for me to slap my forehead.

I have several WKInterfaceControllers with each implementing WCSessionDelegate for communication between iOS application and watch extension. No problem. However when navigating back to my initial WKInterfaceController (using the back arrow the watch OS provides after having pushed:

pushControllerWithName("presetView", context: thisList[currentIndex])

My communication to the iOS application was broken. I thought it might have something to do with the button @IBAction method or something… but since we don’t have a setTarget for WKInterfaceButtons, that wasn’t it.

I initially set all that up the communication in the awakeWithContext method. Guess what? When you navigate back, that’s been purged. If you also set it up in willActivate, you’ll be good.

So in both my override func willActivate() and override func awakeWithContext(context: AnyObject?) I have this code (supporting class variable):

if WCSession.isSupported() {
    session = WCSession.defaultSession()
    session.delegate = self
    session.activateSession()
}

Problem solved. When navigating back, the session object is created again as it needs to be for calls on session to operate.

One thought on “WatchConnectivity gotcha. willActivate()

  1. well, maybe it would be better to have a separate object that would manage the communication. Activating the session whenever an interface controller appears doesn’t seem like a good idea to me.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.