For the last few years, I’ve been working on a small hobby app that runs in the background on the Apple Watch. Keeping an app running on the watch has proved challenging in a number of ways — Apple imposes strict limits on third-party watch apps, partly to preserve battery life, and partly to limit what a malicious app can do. Here’s what I’ve found out about those limits and how to work within them.
Starting a watch app
A watch app that is not running can only be started in one of these ways:
- explicitly by the user on the watch. You can prompt the user to do this by scheduling a local notification and hoping the user taps on the notification to launch the app. If your app has no server, and the user has not put the app’s complication on the watch face, this is the only option.
- in response to a notification received by APNS from a remote server. I don’t think you are allowed to send these from a phone app, so you would need a remote server to do this. I’ve not experimented with this approach at all.
- if the app has a complication on the currently-active watch face:
- the phone app can send a message to the watch using transferCurrentComplicationUserInfo() — this will launch the watch app in the background to receive the message. There is a quota for how many of these calls can be made in a day — probably 50-60 times per day. If there is no complication, or the quota is exceeded, this message is just sent as transferUserInfo() and will be queued until the app is next running.
- the watch app can schedule a background refresh to be executed in the future, but see below.
Background tasks
A watch app can remain running in the background if it registers for location updates, for example using CLLocationManager.startUpdatingLocation(). While monitoring location, the watch will display a dot at the top of the screen (a miniature, circular, version of the app’s icon). This dot is draw last, so it can hide other useful indicator dots.
If the user has granted Always location permission, the app can start location monitoring from a background task. If the user has only granted In Use permission, location tracking must be started when the watch app is in the foregound.
While running in the background:
- So long as the app is still running in the background, delayed execution using (for example) DispatchQueue.asyncAfter()will work. But there is no guarantee that watchOS will not terminate the app and relaunch it for the next location update if there is memory pressure, in which case, pending timers will be lost.
- a watch app can schedule a background task with scheduleBackgroundRefresh(). This will launch the app in the background if it is not already running, but there are two limitations:
- if the app has a complication on the currently-active watch face, there is a small quota for these — approximately 4 or 5 per day. If there’s no complication, the quota is zero and this call will do nothing
- There is a 2 second limit on CPU time for this call, and your app will be terminated if you exceed it.
- if your app is already running in the background, a WatchConnectivity transferUserInfo() call will cause it to run. There is a 2 second limit on CPU time for this as well.
- A location update will cause your app to run. There is a longer CPU limit in this case — I’ve not hit it myself. I’ve seen in crash logs for other apps that heart rate notifications have a 15-second CPU limit, so it might be similar to that. However, you need to be careful not to process location updates during a background refresh or watch connectivity session, as those 2-second limits will apply in this case.
Sleep Focus mode
When the watch enters Sleep Focus mode (for people who wear their watch to sleep), the screen is turned off (blank and disabled). If there is a watch app running in the background — for example, actively monitoring location updates — this does not happen. The screen remains on and tappable. You can only solve this by stopping monitoring while the watch is in sleep focus mode. However, this is problematic, because:
- there isn’t a way for the watch app to find out when sleep focus starts or stops.
- if you stop location tracking, there isn’t a simple way to restart tracking the next morning (see above).
One possible solution is to implement a Focus Filter extension in the iphone companion app, but this is still fiddly:
- The user needs to go to the settings app to add your app as a focus filter for sleep mode.
- The iPhone app will then be notified when sleep mode starts and stops
- When sleep mode starts, the phone app can use transferUserInfo to ensure that the watch app stops location updates
- When sleep mode ends, the phone app can only wake the watch app if there is a complication on the watch face.
For my own app, I have a user-configurable sleep period. The watch stops tracking at the start of the sleep period. To restart, it uses a background refresh, which works if there is a visible complication. In case that doesn’t work, the app posts a local notification prompting the user to tap on the notification to launch the app. This feels very clunky and hard to explain.
One interesting observation: if the app is launched in the background during sleep mode and starts tracking (with Alwaystracking and a complication visible), the screen remains blank and the tracking-activity dot is not shown until the user explicitly opens the app. This seems to be an Apple bug rather than deliberate, but is true on the latest software (watchOS 27.0).
Recency and Frequency
Apple’s software pays attention to when and how often the user opens your app:
- if you tap on the notification to launch the app every morning at 7am, watchOS will start to launch the app in the background a little while beforehand, to allow it to refresh itself. This is sufficient to start tracking if the user has granted Always permisson. Of course, then you won’t need to launch the app since it is already tracking locations, and then after a while, watchOS will stop launching it.
- If the app is running in the background (on watch or phone) and you don’t open it in the foreground occasionally (once a day or two?), the app will be quietly terminated until the user explicitly relaunches it. At the risk of seeming very needy, you could schedule a local notification notifying the user that the app is not running,, clearing it and reschedulng it for the future if the app is still active.