Supermarket automatic doors open for me; therefore, I am.

Craig Bruce

Chapter 3
Automating with Geofences

The smartphones we carry around all day would look like a magic device to someone from thirty years ago. Like a magician, we can predict the future (“Is it likely to rain in an hour?”), we can make things move (“Siri, open the front door”), and we can make something happen whenever we enter a place (“Turn heating on when I’m home”).

We’re used to it, but it’s still fascinating, as it all seems to happen automatically. But it doesn’t—underlying code gets executed because of some trigger. We can’t see the trigger, and that’s why it feels a bit like magic.

In this chapter we’ll use such a “magic” trigger to execute our code. We’ll build an app that measures how long users are outside each day without the need for any interaction from them. The app will register when the users leave their home and when they come back, and it will show a chart of the hours they spend outside each day.

The trigger we’ll use is a geofence. A geofence is a region on a map that’s coupled with an automatic action that executes when the device with the geofence enters or exits the region. In iOS, geofences are one of the few supported ways to execute code when the app in question isn’t running. When the device with our app enters or exits the registered region, our app is launched in the background and can execute code for a few seconds before it’s terminated.

Geofences can even be useful for apps that don’t use any other location services. For example, some RSS reader and podcast apps fetch the latest items based on geofences. And years ago I used geofences to store the iPhone’s battery level to estimate how long the battery would last based on the phone’s usage. I’m sure you can come up with other use cases for a geofence, but first let’s see how one works.

Get your magic wand and let’s begin.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.10.137