Apple watch complications update limit - apple-watch

I have a complication that may need to update every 5 minutes. this could easily sum up to 120 updates per day. Is there a way to only update when the user wake up the watch?

I belief the answer to your question in NO, there is currently no way to only update the complication when the user wakes up the watch.
The reasons:
The idea of a complication is that the user sees it as soon as he or she raises the watch and activates the display.
There is simply no time to activate or launch an app, nor to download a real-time value to display it as a complication.
Thus, the data must be present before the user watches the watch face with the complication.
In many cases, this can be ensured, if the data don’t change too often:
On watchOS, it is possible to schedule background tasks at certain intervals that download actual data from a server. In your case, you would like to schedule regularly (in your case about every 5 min) a WKApplicationRefreshBackgroundTask to download the actual value, but due to power consumption reasons, the number of tasks is limited:
Background app refresh tasks are budgeted. In general, the system
performs approximately one task per hour for each app in the dock
(including the most recently used app). This budget is shared among
all apps on the dock. The system performs multiple tasks an hour for
each app with a complication on the active watch face. This budget is
shared among all complications on the watch face. After you exhaust
the budget, the system delays your requests until more time becomes
available.
So there is no way to do this at rate of 1 per 5 min on watchOS.
One could try to do this via iOS, and send the new complication data using func transferCurrentComplicationUserInfo(_ userInfo: [String : Any] = [:]) -> WCSessionUserInfoTransfer, but the docs say:
If the complication is on the active watch face, you are given 50
transfers a day.
So I do not see any way to preload complication data at this rate before a user raises the watch.

Related

Having complication data up to date when the user unlocks their watch?

I'm currently working on a simple app that displays data received over the network in a watchOS complication. Notably, this data is only relevant for ~30 minutes before a new network fetch is required.
I'd like to have the complication be up to date when the user unlocks their watch in the morning (this is a common use case presented by Apple).
Is it possible to receive some kind of background task when the user unlocks their watch? If I schedule a background task and the watch is locked and charging when the refresh happens, will the background task still fire? What techniques can I use to have data ready for the user when they wake up and unlock their watch? Is there documentation specifically focusing on background tasks when the watch is locked?
As far as I know, the watch works in its locked state only slightly differently from its unlock state.
One difference is the display of complications:
You can specify the privacy behaviour, i.e. what the clock face displays as complication (you can select what is displayed on the lock screen).
So, to my mind, it is possible to run background tasks as scheduled when the watch is locked and charging. Thus the data will automatically be ready when the watch is unlocked.
For this reason, there is no special documentation what happens when the watch is locked, except for some special cases, as what is displayed on the watch face in the locked state.
I would use the app life cycle docs here, and quite possibly choose:
in applicationDidEnterBackground(), I'd set a flag (time when the
complication was last updated). I'd suggest you use a singleton, so that it's accessible anywhere in your app.
then in applicationDidBecomeActive() i'd pick up the flag, compare it
with the current time, and notify the active ViewController to
refresh its data, if it's greater than 30 minutes.
if the flag doesn't exist, because the app was terminated, or it's a
first launch, then refresh anyway (set a 24h date in the past, to
use the same logic as in 1/,2/)
if you want to make it more permanent, use NSDefaults to store the last time the complication was updated.

Requesting location in watchOS and its effects on battery

I am trying to create a low-power watchOS app that will only periodically (every 5 minutes or so) need an updated but precise location. The key word there is need. The app certainly could provide better information if it were getting location updates consistently but the goal is for this app to be low power. That being said (and I am not sure if this is possible) if another app is constantly streaming location in the background it would be nice to get those updates too if that does not have extra battery cost.
Is there a way to only request location periodically? Is this actually better for the energy impact of the app?

watchOS get activity data

How can I go about capturing daily activity data from Apple Watch at a high level (gyroscope and accelerometer data) or is that not possible (due to current limitations of the API)?
I am trying to figure out a way to find all occurrences of a custom workout performed during the day by a watch user. User installs the app, grants permissions where applicable. Launches daily to see workouts performed in the last day. The app pulls data, stores it, analyzes it and plots necessary metrics.
I want to know if this is possible - pulling data on launch to run analysis. If so, how?
I see this example from Apple on recording counts swings in a racquet sport but that seems to require instantiating and stopping various CoreMotion events such as startDeviceMotionUpdates and stopDeviceMotionUpdates in order for the app to retrieve them.
Any pointers?

Measuring heartrate while Apple Watch is in sleep mode [duplicate]

I need to make an app that records heart rate data in near real time and send this data to a server as soon as possible.
First I took this approach: Watch os 2.0 beta: access heart beat rate
In fact it is working fine. There is new heart rate data in the HealthKit every five seconds. But now I have the problem that I can't sync that with a server.
My first approach was the Watch app. The watch was sending data to a server. That doesn't work because as soon as the screen turns black on the watch, it stops sending.
My next approach was to query the HealthKit on the iPhone every five seconds for new data. This works, as long as the app is in foreground.
Then I saw that there's some kind of background functionality that watches the HealthKit itself and revokes the app from background and you can do something.(enableBackgroundDeliveryForType) This doesn't seem to work for heart rate (the Apple Documentation says for things like steps this doesn't work, I guess heart rate is one of those).
I'm stuck now. Do you know how to it? I would need some background task that is executed every 5-10 seconds on the iPhone. That seems to be impossible
UPDATE
As noticed by #BootMaker, Apple made background mode available for HKWorkout apps in WatchOS 3, so it's working now. You have to run a HKWorkoutSession and this will keep your heartrate delivery in real time even when the app is in the background (dark screen on watch)
The closest you are going to be is while the watch app is open.
Why I'm stating this?
There are two HealthKit's Database (one at the iPhone and another at the Apple Watch). When they sync is arbitrary and decided only by the O.S.
The closest you are going to be to real time is when you don't have any password locking your screen in iPhone or Apple Watch.
Either way, there's no guarantee that the sync will happen every time a new measure is added to Apple Watch's HealthKit
The only way to force the Heart rate sensor into working in real time is via workouts or observer while your Apple Watch app is in FOREGROUND.
Background delivery is NOT available for Apple Watch apps.
Watch OS 2 request the sensor to measure automatically (in background) every 10 minutes minimum.
There's no other workaround, if you need real time for longer periods, or while the user is not using your app, you will need to use an specialized wearable.
If anyone still need to get heart rate or other data in real time. Use this solution:
Develop an apple watch app/extention
In watch app, using HKHealthStore, HKWorkoutConfiguration, HKWorkoutSession, HKLiveWorkoutBuilder to create an Workout. After create workout, your watch app will get heart rate in real time.
Using watch kit connection with WCSession to send data to iPhone app.
Enable background mode both in apple watch and iPhone.
I tested, even app terminated, we can still get heart rate (I used Local notification for posting heart rate data for debugging)

How can an iOS app run a function in the background in a recurring manner (i.e. once an hour)?

On devices which support multitasking, I'd like my iOS app to run a function in the 'background' when the app is not running (i.e. it's suspended). I know that iOS supports running tasks in the background, but I'm not sure how to make the function recurring (and only when the app is not in the background). What's the best approach?
I'm not interested in running a long-term function in the background but a short-term function to simply update the application badge #. However this number is dynamically based on the app's data, and needs to run a query against core data.
To further clarify, yes, my core data will not be changing, but the badge represents a number of items due. As time progresses, more items will be due, so I want to update the badge to show the proper items due as time progresses. So if 5 items are due now, but half an hour later 3 more items are due, then by the time the next hour comes around, 8 items will be due even though the core data has not changed at all in and of itself.
It can't, the only task allowed to run in background are: audio, voip and location.
Why do you need to update the badge data every hour if the data stored within Core Data is not changing? ie. the app isn't running?
You can do this using push notifications, like previously posted, or you can use a scheduled local notification based on the data when the app is closed or backgrounded. I think those are pretty well your only options.