iOS Widgets

2 min read

iOS widgets allow an app to provide instant access to meaningful data. They stay on the phone's home panel acting as glanceable windows into your app. 

Image of the medium Calendar widget displaying the month of June in the right half and an event titled Ivy Calhoun’s Birthday in the left half. June seventeenth is highlighted in red in the month view and a small glyph of a gift is displayed in red to the left of the event title.

Simon Støvring's Scriptable uses iOS widget concept to enable iOS automation written in ECMAScript 6. Scriptable acts as the parent container app for your custom Javascript widget. 

iOS reloads widgets periodically to update widget data. Update frequency depends on your usage pattern with faster reloads for frequently used apps. When your script uses location services, iOS reloads it after a significant location change.

This got me thinking - can I use the significant location change service to build a mileage expenses logger as a widget? The logic would be something like:

  • Record location coordinates whenever the widget starts.
  • Significant-location change will trigger subsequent widget restarts.
  • When the gap between two coordinate timestamps is wider than x minutes, assume the end of a trip and start of another. 
  • Reverse geocode start and end coordinates to record from/to addresses. Scriptable uses apple map for this.
  • Write out the trip record with the route path into a file in iCloud. This file is your persistent mileage log. 

A quick prototype script is at https://gist.github.com/joyanujoy/19831b8849848f999e71333f15b94d40

Widget UI with light and dark modes:

The experiment didn't work as I had hoped. It appears iOS stops location updates to the widget if you haven't used it recently. See Accessing Location Information in Widgets. The need for opening the widget frequently prevents it from acting as a background geolocation logger.

Looks like I will need to build a native iOS app for logging mileage in the background. On the plus side, I could use the app to demo my geo filter API.

scriptable background geolocation