0

I have a IOS application with a companion WatchOS application. I am wondering if there is a way I can communicate with the IOS application in real time while it not running as a foreground application.

The best example I can think of is how your are able to use the Spotify watch app to change songs, like songs or even change the volume of the phone while the phone is locked.

How could I implement something like this for an application that could possibly send data frequently from the watch to the phone while the phone is locked.

2 Answers2

1

I just started WatchOS development and needed to send a message to the watch app - if it's available I use sendMessage. If the watch app is backgrounded, I use transferUserInfo. Link to code snippets. I check isReachable and isPaired (on the phone, isPaired does not exist in WatchOS).

0

Watch apps can launch their companion iOS apps by messaging them:. https://developer.apple.com/documentation/watchconnectivity/wcsession/1615687-sendmessage

That won’t work if the iOS device requires unlock because it’s just booted up.

Shadowrun
  • 3,572
  • 1
  • 15
  • 13
  • I appreciate your answer. I am not looking for how to launch a companion app, instead a way to communicate in real time to the companion app while one of the applications are in the background. I mean the application is already open, like the Spotify example, I lock the phone but on the watch app I am able to like, skip or pause a song. How is this communication achieved immediately and not scheduled as some background task? – Matt Braniff Apr 03 '21 at 08:20
  • sendMessage(_:replyHandler:errorHandler:) will do it, this is called Interactive messaging. Even if the iOS app isn’t running, it will be launched (into background) to handle the message from the watch, and optionally send replies. If it’s running already, no problem – Shadowrun Apr 03 '21 at 08:29
  • Note that this only works one way, you can’t launch the watch app from the phone, except via a special “start workout” API. So from iPhone, you can interactively message the watch app only if it’s already running – Shadowrun Apr 03 '21 at 08:32
  • Interesting I always thought sendMessage required an active session from both! Thank you – Matt Braniff Apr 03 '21 at 08:51
  • For the messages to get through, yes, but just be sure to set a delegate and call WCSession.default activate in didFinishLaunchingWithOptions. I guess if you don’t you will be launched and then soon you just get suspended again? – Shadowrun Apr 03 '21 at 09:13