Reader small image

You're reading from  Learning ibeacon

Product typeBook
Published inNov 2014
Publisher
ISBN-139781784397128
Edition1st Edition
Right arrow
Author (1)
Craig Gilchrist
Craig Gilchrist
author image
Craig Gilchrist

Craig Gilchrist is the Digital Director at Eden Agency (http://createdineden.com/), a small but mighty digital agency that is at the forefront of proximity marketing. His team is one of the first to release commercial iBeacon-powered apps in the world and currently boast over 1.5 million app downloads in multiple fields and have a perfectly balanced technical and creative team. Craig is based in North Yorkshire, England, and has been developing commercial software since graduating from the University of Teesside in 2004 with a BSc in Software Engineering and has been building apps for iOS since 2009. Craig is an avid reader and is always at the forefront of commercial technological developments. Other than mobile app development and digital marketing, Craig also has a keen interest in developing rich media, including gaming, children's interactive media, and a very keen interest in Unity and virtual reality with Oculus Rift.
Read more about Craig Gilchrist

Right arrow

Chapter 5. Detecting Beacons in the Background – Location Dating

So far, we have talked about discovering beacons, ranging beacons, and even using our iOS device to broadcast as a beacon. You should be feeling pretty empowered right now. All of these use cases, however, require your app to be running.

Having a running app for every use case isn't realistic. Considering that iBeacons are basically triggers for functionality, it makes sense that some of this functionality might be to bring the app into the foreground. In this chapter, we'll explore some other use cases where the app might be running in the background and is brought to life when a beacon region is entered.

To demonstrate background beacon detection, we'll create a location-based dating app that notifies the user when a potential date is nearby.

We'll cover the following topics:

  • Monitoring for beacons in the background

  • iOS architecture to defer region monitoring to the operating system

  • Different scenarios for background monitoring...

Real-life use cases


If you think about it, most usages of iBeacons won't always involve the app running in the foreground. Most apps are most likely to be awakened when entering the boundaries of a beacon region and then brought into the foreground by the user if they want to use the app.

An example use case for retail loyalty

Imagine that you're building an app for retail loyalty. In this scenario, you are almost certainly going to want to trigger some functionality when the user comes into the range of your store. You might want to send the customer a tasty offer for your new bagel range, or simply offer users loyalty cards just in case they want to pop in for coffee.

An example use case for airline assistance

Now, ponder building an app for an airline. Your app allows users to book their flight and download their boarding pass. The app also allows the user to get departure lounge discounts and directs them around the airport.

Once you book your flight using the app, it becomes completely useless...

Handing over responsibility


In iOS, regions (CLBeaconRegion or CLRegion) associated with your app are tracked all the time, including when the app isn't running. If a region boundary is crossed while an app isn't running, the app is relaunched in the background to handle the event.

Similarly, if the app is suspended when the event occurs, it's woken up and given a short amount of time (around 10 seconds) to handle the event.

When necessary, an app can request more background execution time using the beginBackgroundTaskWithExpirationHandler: method of the UIApplication class.

This means that your app can perform a few actions such as showing a local notification or sending an HTTP request (or both), but it can't really perform running actions such as ranging beacons any longer; this is because once your app goes back to sleep, this function is stopped being called.

You can perform longer running actions in the background by turning on the background modes in the Capabilities tab of the application...

The CLBeaconRegion options


The properties of the CLBeaconRegion instance have a huge impact on how your app behaves when running in the background. Let's explore a few of these options now:

  • CLBeaconRegion.notifyOnEntry: When this property is YES, a device crossing from the outside to the inside of the region triggers the delivery of a notification. If the property is NO, a notification is not generated. If the app is not running when a boundary crossing occurs, the system launches the app in the background to handle it.

  • CLBeaconRegion.notifyOnExit: This property works in a similar way as mentioned in the preceding point, except for the notification that occurs when the device crosses from the inside to the outside of the region. There is usually a delay of up to 10 seconds after the device has fallen out of range of the last beacon in the region, but this can also be up to 30 seconds. This cushion time is to ensure that numerous entered and exited events are not called in quick succession...

Passbook integration


Wouldn't it be great if beacons worked directly with Apple's e-wallet solution passbook? Of course it would, and of course Apple has thought of this. You can easily bring your passbook passes to the foreground when in the range of a beacon.

As of iOS 7.0, Apple added the beacon dictionary keys to the PassKit bundle, giving the ability to define activating beacons alongside text to display when in the range of a particular beacon.

With the new dictionary values, you can specify an array of beacons that show a message and a thumbnail image of the pass, which allows you to bring the pass to the forefront without unlocking your phone, as shown in the following figure:

Showing passbook passes from the lock screen using iBeacon

Exploring PassKit (Apple's tool to create passes) is definitely beyond the scope of this book, so we'll only skim over the structure of a pass. It's enough to know that a pass is made up of a ZIP file with the file extension .pkpass, which contains the...

Our tutorial app


We will now create an app that demonstrates all of these features; it will monitor regions in the background before presenting a local notification. When the app is running, we'll range the beacons in our region and stop ranging them when the app drops into the background. Finally, we'll allow the user to add a Ticket to love passbook event ticket pass that will show up on the lock screen when it's within a region.

To test our app, we'll use the companion OS X application to advertise one of two beacons, representing either a "hot guy" or a "hot gal".

The scenario

You've been asked to build a new location-based dating app. When lonely hearts sign up to the service, they are sent an iBeacon key ring that they carry around with them, and they also get to download a companion app that lets them know when other lonely hearts are in the area.

The lonely heart key rings use the same UUID; however, hot gals broadcast the major value of 1 while hot guys broadcast the major value of...

Testing your application


We have finished building the app, and it's time to test. There's a lot of code in this tutorial, so don't be disheartened if it doesn't work the first time.

Testing the beacons

Just go through the steps again to see where you went wrong, or review the complete code from the download resources. Use the following steps:

  1. Tap guyButton before pressing the device's home button, thus sending the app to the background.

  2. Now, using the companion OS X app, start the beacon entitled Chapter 5: The hot guy, as shown in the upcoming figure. You should be presented with a push notification.

  3. Start the app, and you should see the distance being updated in distanceBarButtonItem.

  4. Press the home button again before turning off the beacon in the companion app.

  5. Wait for up to 15 seconds for the exit region event to fire, and again, you should be presented with a different push notification notifying you that you've exited the region.

  6. Perform steps 1 to 5 for the gal.

Running the companion application...

Summary


Congratulations, you're now an iBeacon guru. In this chapter, we completed our knowledge of CLBeaconRegion and its properties, which affect background behaviors such as notifyEntryStateOnDisplay and notifyOnEntry.

We discussed the limitations of functionality when our app is activated by an OS when entering a region, and why we shouldn't range beacons in the background.

We also discovered how we can bring the passbook passes to the front of the lock screen using iBeacons.

In the next chapter, we'll expand our knowledge of what to do when we leave a region, and we'll discuss home automation and the implications of iBeacon upon that field.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning ibeacon
Published in: Nov 2014Publisher: ISBN-13: 9781784397128
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Craig Gilchrist

Craig Gilchrist is the Digital Director at Eden Agency (http://createdineden.com/), a small but mighty digital agency that is at the forefront of proximity marketing. His team is one of the first to release commercial iBeacon-powered apps in the world and currently boast over 1.5 million app downloads in multiple fields and have a perfectly balanced technical and creative team. Craig is based in North Yorkshire, England, and has been developing commercial software since graduating from the University of Teesside in 2004 with a BSc in Software Engineering and has been building apps for iOS since 2009. Craig is an avid reader and is always at the forefront of commercial technological developments. Other than mobile app development and digital marketing, Craig also has a keen interest in developing rich media, including gaming, children's interactive media, and a very keen interest in Unity and virtual reality with Oculus Rift.
Read more about Craig Gilchrist