How to wake iphone on applewatch programmatically - ios

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?

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.

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.

Related

A way to selectively deliver UILocalNotifications to iPhone vs Apple Watch?

I'm trying to create a feature where certain UILocalNotifications trigger a sound despite the fact that they are displayed in an Apple Watch, or if that's not possible, are not routed at all to the Apple Watch and just to an iPhone where they can play a sound. Having the watch produce a sound is not ideal compared to the iPhone since I need it to overlay any music the user is playing on headphones.
Obviously the user can disable notifications on the Apple Watch for the app, but there are cases where it is useful to receive notifications on the Apple Watch and that is an all-or-nothing solution.
The Apple documentation states:
When one of your app’s local or remote notifications arrives on the user’s iPhone, iOS decides whether to display that notification on the iPhone or on the Apple Watch.
Is there any information on how iOS decides where to display a notification? Is there any programmatic way to disable notification forwarding to Apple Watch?
You can selectively disable notification in apple watch: http://www.imore.com/how-turn-alerts-apps-apple-watch
See the answer in below link for app’s local or remote notifications guide:
https://stackoverflow.com/a/33581623/2401116

Wakeup Watch app OS2 from the parent IOS app?

I would like to give a haptic feedback on the watch. That works fine as long as the watch app is active, but if the watch app goes in background the haptic feedback is not played. Is there a possibility to "wake up" the watch app from the parent IOS app to play the haptic feedback? (NOT A NOTIFICATION)
Since you prohibit Notifications, is not possible to do what you describe. Your watch app can act on messages from the iOS only when it starts up, resumes, or is currently loaded and running.
WatchConnectivity gives you 5 ways of sending data between the watch and phone.
sendMessage, sendMessageData: your scenario excludes these calls since both the watch and iPhone apps need to be active for this mechanism to work.
updateApplicationContext, transferUserData, and transferFile can be invoked from the iOS app at anytime, regardless of the state of your watch app. However, these messages are sent on a background thread and your watch app delegate will receive them the next time the watch app is loaded. By the design of the watch OS, none of these methods can trigger the watch app to load or become active.

Understanding interaction between Apple Watch and iPhone device

I am a bit confused as when I run the Apple Watch extension App schema on the simulator I see only that the Apple Watch app gets lunched. However I am not sure what will happen with real hardware once we get our hands on the Apple Watch.
I'd like to understand the following:
Do I need to start the corresponding iPhone App on the phone before
starting the App on the Apple Watch?
If the answer to 1 is no then, can I
start an iPhone App from the App on the Apple Watch? If the answer to 2
is yes then how can I start the iPhone App in a way that is not in
the background? (I found handleWatchKitExtensionRequest can trigger
background tasks but I haven't found any documentation yet for
that - I also found the Handoff Api which allows you to resume a task on the device but I don't like that it requires an iCloud account and I am not sure how to structure an App/watch interaction without forcing the user to adopt iCloud).
Given that the Apple Watch App extension resides on the iPhone App
bundle does this mean that when you start the Apple Watch App it needs a
connection and proximity with the iPhone device? I assume that there will
be a bluetooth connection and that it won't be possible to start
some apps if the user and its watch are away from the Iphone (what's
the range?)
Many Thanks
No.
No.
Unless you are on a Wifi network recognized by both your Watch and your iPhone, they'll need to be relatively close to each other. Guessing at range is useless – a number of factors can impact the useable distance of BTLE.

Bring iOS App to Foreground in WatchKit

I am developing a WatchKit extension for one of my Apps where I really want to have the iPhone App running in the foreground, since it is doing the heavy lifting and is designed to use location services only when active. I know openParentApplication:reply only opens the App in the background (unless it is already active). What I am currently trying out is using a custom URL scheme for my App, and having the App initially open itself from within the handleWatchKitExtension code in the App Delegate. This works perfectly in the Simulator. Unfortunately I did not have this approach ready when I tested my App on real Apple Watches. Does anyone see a problem with this approach to bring the iPhone App to the foreground from the Apple Watch?
On iOS, apps can't bring themselves to the foreground. The best thing you can do is to post a local notification (UILocalNotification, see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html) and invite the user to bring your app to the foreground.
But for your use case, why not just have your phone app use location services in the background?
This is not possible. The Apple Watch Programming Guide states that the app on the iPhone will run in the background if it was not active before.

Are iPhone's apps notifications going to be triggered on the Apple Watch by default, without even creating an actual Watch app?

I would like to support my iPhone's app Local Notifications in the Apple Watch from the very first moment the Watch is released, but I don't plan on building a Watch App yet.
From what I researched, it seems like iPhone's notifications will be automatically supported on the Watch, without needing any additional development or WatchKit implementation.
Apps are not required to do anything to support notifications. The system provides a default notification interface that displays the alert message from the notification. However, apps can customize the notification interface and include custom graphics, content, and branding.
Is this correct?
If so:
1. Will notifications on the Watch have action buttons just like configured for the iPhone app?
2. Is there any way I can test that on the simulator to be sure it works as expected?
If not:
3. Is there any way to support only notifications without creating a Watch app?
This is the more related question I found so far, but it's not really what I'm looking for. How to trigger notifications using WatchKit
I would like to support my iPhone's app Local Notifications in the Apple Watch from the very first moment the Watch is released
Yes notifications for your iOS application will work from day one on Apple Watch.
Will notifications on the Watch have action buttons just like
configured for the iPhone app?
If your iOS app already supports actionable notifications they will work with Apple Watch.
Is there any way I can test that on the simulator to be sure it works
as expected?
Good question actually, you can't test your current iOS app's notifications working on the simulator but you can test a custom notification interface from WatchKit.
You should file a feature request for this.
Is there any way to support only notifications without creating a Watch app?
No, you'll need to provide a sample Notification JSON payload as part of an WatchKit Bundle, which requires submitting an Apple Watch app.

Resources