swiftwatchkitwatchos-2wcsessionwkinterfacecontroller

WatchKit InterfaceController conditionally play haptic in sendMessage:replyHandler:


I want to make the interface controller check to make sure it is still in the foreground when I get a reply from the phone. If it is, perform a haptic to let the user know it finished working.

sendMessage:replyHandler: is what I am using to communicate to the phone, but it can take a long time to return. The InterfaceController is set up to be the WCSessionDelegate. It can take long enough that the user could lower their wrist before completion. In that case, I want to trap the haptic not to fire. As of now, the haptic can queue up and then play at a random time in the future, which is not helpful.

I have configured a notification to be sent, but that only displays if the watch isn't showing the app. Hence why I need to figure out of the interfacecontroller is on screen before trying to play the haptic.


Solution

  • I'd first think to look into improving the iOS code, so it doesn't take as long to return a reply.

    However you can include the current date in the reply message, then have the reply handler check to see if the reply was immediate before playing the haptic. This would avoid it from playing in the future if the reply was handled when your app next became active.

    By the way, the interface controller would be on-screen, since it is executing the reply handler.