0

My scenario is control iphone music player on applewatch. Just like what the applewatch music glance would do. The project is gonna be iphone app, watch app, and watch extension. however, I want it to be able to work even thou my iphone app is not active. I know when iphone app is active, I could use wcsession and sendMessage to control the music on iphone. However, when the iphone app is not active. I don't know what I should do to get the work done.

One more thing, I don't understand how iphone app works when it is not active. Does it need to be active first do those job, or it is never really inactive so it could still do work?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jay Hu
  • 6,041
  • 3
  • 22
  • 33

2 Answers2

1

All you have to do is configure your App to respond to remote control events.

Remote Control events Remote control events are any event received for the purpose of controlling media. eg iTunes pause/play/next/previous buttons available from the control center, or remote-control events from play/pause buttons on headphones.

Here is a tutorial on the subject.

Then your watch glance will be able to control the Apps audio.

With the current API as of Watch OS2 and iOS9, rewriting an glance similar to the now playing glance is not possible.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • maybe you misunderstood me abit. I didnt mean i want a ios app that could be controlled by watch music glance. i want a watch os 2 app could controlled iphone's music even its parent ios app is in background – Jay Hu Jun 25 '15 at 16:40
  • how about it is not a glance. What if it resides in interface controller. Could that happen? – Jay Hu Jun 25 '15 at 18:11
  • Unfortunately not, you cannot effect change on the system MPMusicPlayerController instance. – Woodstock Jun 25 '15 at 18:17
  • I understand that watchkit cannot do that. But iOS could control music even in background just like that tutorial. So how about I use watchkit to ask my iOS app to do it for me? That was why I mention WCSession. However, I think WCSession only works in foreground. – Jay Hu Jun 25 '15 at 18:30
  • Interesting, reading the docs it seems this may be possible with the background messaging calls... https://developer.apple.com/library/prerelease/watchos/documentation/WatchConnectivity/Reference/WCSession_class/index.html#//apple_ref/doc/uid/TP40015237-CH1-SW43 – Woodstock Jun 25 '15 at 18:36
  • You are right. I have been work with this WCSession intensively. However, I noticed that You can only sendMessage when both watch app and iOS app are active, or you could transfer data from one to the other for the later use when user manually activate the app. However, you cannot programmatically wake up the counter part to do work. Therefore, if my iOS app is in background, my watch app won't be able to control the music anymore. – Jay Hu Jun 25 '15 at 18:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81569/discussion-between-sharppug-and-woodstock). – Jay Hu Jun 25 '15 at 18:51
  • Just for the sake of clarity, the watchOS app could awake the iOS counterpart but the iOS app couldn't do it with its watch app. – BootMaker Feb 07 '16 at 07:41
1

Use this to wake up your iPhone App from your watch kit App in the background:

from https://developer.apple.com/library/prerelease/watchos/documentation/General/Conceptual/AppleWatch2TransitionGuide/UpdatetheAppCode.html

Interactive messaging mode

sendMessage:replyHandler:errorHandler:

sendMessageData:replyHandler:errorHandler:

Use this mode to send data immediately to a counterpart app. If you do not want a response, you may specify nil for the replyHandler parameter. The counterpart app must be reachable before calling this method. The iOS app is always considered reachable, and calling this method from your Watch app wakes up the iOS app in the background as needed. The Watch app is considered reachable only while it is installed and running. Data is transmitted immediately and messages are queued and delivered in the order in which they were sent.

Gerd Castan
  • 6,275
  • 3
  • 44
  • 89