Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I keep a Watch app running in background?

I know iOS allows background tasks to run and, for example, continue to receive location updates, but is it possible to do this in a watch app? In Xcode 9.3, I have configured my app for "background modes" and selected location, and that has created for the WatchKitExtension's an Info.plist, an entry for "Required background modes" of "App registers for location updates".

But my watch app still suspends when the screen turns off, and when it is in the dock.

The App Programming Guide for watchOS, however seems to exclude the possibility of running in the background to receive location updates as it only allows background processing for four classes of activity:

  1. Background App Refresh Tasks. Use a WKApplicationRefreshBackgroundTask object to ...
  2. Background Snapshot Refresh Tasks. Use a WKSnapshotRefreshBackgroundTask object to update ...
  3. Background Watch Connectivity Tasks. Use a WKWatchConnectivityRefreshBackgroundTask object to receive data sent by your iOS app ...
  4. Background NSURLSession Tasks. ...

Other posts to SO indicate it's not possible, but proving a negative is difficult, so I'm asking again:

Am I "flogging a dead horse" by trying to keep the watch App operating in the background for receiving location updates, or is Xcode is making promises that WatchOS won't deliver.

like image 741
alan.raceQs Avatar asked Dec 19 '17 23:12

alan.raceQs


1 Answers

I'm delighted to be able to report that the horse I have been flogging for the past two weeks was not dead after all!

I have discovered an additional state in which my watch app will continue to run in the Background which does not require HKWorkoutSession.

These settings did the trick:

locationManager.allowsBackgroundLocationUpdates = true

and in watchKitExtension info.plist:

set UIBackgroundModes (Required background modes) to location (App registers for location updates)

And I repeat: I am NOT using healthKit

Now my app continues to run even when the screen is off and when the app is out of the Dock.

like image 138
StarTraX Avatar answered Oct 13 '22 04:10

StarTraX