Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get Apple Watch timer app to run in background while the screen is off

I am trying to build a timing app for the watch that vibrates every minute just as a way for the user to keep track of time. I have the timer correctly working and vibrating, but once the screen on the watch goes off, it no longer vibrates. Once I wake the screen back up, it goes back to normal.

Right now to send the vibration I am using this:

WKInterfaceDevice.currentDevice().playHaptic(.Stop)

I know this might be some sort of design implementation to save battery life, but obviously the point of my app is to keep the track of time even when the screen is off. Is something like this possible?

like image 552
alexk403 Avatar asked Mar 05 '16 19:03

alexk403


People also ask

How do I get my Apple Watch timer to stay on?

To do this, press the Digital Crown to see the Home screen, then tap the Settings button . Tap Display & Brightness. Scroll down, then tap Always On. Tap Always On to turn on or turn off the feature.

Do apps stay open in background on Apple Watch?

Apple Watch continues to display your app's user interface as long as it's either the frontmost app or running a background session. To preserve battery life, the system updates the user interface at a much lower frequency than when running in the foreground. It also dims the watch.


1 Answers

To keep a watchOS app running after the screen turns off it is now possible to use the new WKExtendedRuntimeSession commands offered in watchOS 6.

Inside your WatchKit Extension ExtensionDelegate file create an extended runtime session with the following command.

let session = WKExtendedRuntimeSession()

Within this delegate file create an applicationDidBecomeActive() function which watchOS will call when your app becomes active. Inside this function start the ExtendedRuntime Session with the following commands.

session.delegate = self   // self as session handler
session.start()  // start WKExtendedRuntimeSession

An extended runtime session must be started when the app is in the active state.

Note you also need to enable Background Modes within the WatchKit Extension target. Select one of the Session Types that most closely fits the type of app you are creating.

like image 172
Sid Avatar answered Sep 23 '22 23:09

Sid