Imagine working in an office which looks like an endless corridor and, unfortunately, your room is at the very end of it. You’re getting hungry, and The Sandwich Guy who should have been to your office a long time ago still hasn’t arrived. After a while, as shocked as you can be, you find out that Mr. Sandwich has already visited your office, but he didn’t knock on your door because he was in a hurry. He only announced his presence in the rooms closest to the entrance. You don’t have lunch which makes you quite upset, so you come to the conclusion that you have to solve this problem somehow. Otherwise, you’ll just starve.
It is a real story, and this is exactly what motivated us to make an app that would help to inform all employees of our office, that our favourite Sandwich Guy arrived. This app has a simple name — JitEat. The goal was to change the flow of the whole process. Instead of visiting each room, The Sandwich Guy will enter the corridor and wait. All the hungry devs will leave their rooms (caves?) to grab food. So, The Sandwich Guy will save a lot of time, which he would otherwise spend knocking on doors and anyone who wants to buy lunch will get a notification about his arrival. To make this process effective, we decided to create an app in which Mr. Sandwich will be able to inform us about his arrival. But, as his time is precious, we didn’t want to make him use a complex UI — a single button was enough. Sending the notification should be available only when The Sandwich Guy enters our corridor. But how can we detect that he is standing exactly there? This is where our small friend appears - his name is Beacon.
In this article I’ll show you how to use beacons in a modern iOS app. All the code samples will be given in Swift programming language.
What are beacons?
Beacons are small devices which communicate with other devices using Bluetooth Low Energy. There are many different types of beacons. Most popular are Proximity Beacons and Location Beacons. The first ones will send a signal when we are inside their range, and the second will communicate with each other, creating a map, thanks to which we will be able to navigate in places where the GPS is unable to show us where we are.
In our case, obviously, Proximity Beacons turned out to be a better choice. We can buy them from a variety of sellers, they differ in appearance, time of operation on batteries, or communication manners. In this article we will cover communication with IBeacon protocol, which is one of the most popular, so you shouldn’t experience difficulties with finding the right beacon.
Let’s start coding!
To start using beacons in our app, we should firstly ask for permission to access the location data. To do that, we need to add new keys to our info.plist file:
Maybe it’s strange that we need to add both of this keys but according to this documentation it’s necessary.
As you can see, after adding these keys, you are able to set string values on the right side where we’ll put message for our users which will explain to them why we need these permissions.
Now we need to add CLLocationManager which will manage our beacon. This manager is a part of CoreLocation framework so in order to use it, we need to add import CoreLocation to our code.
This framework provides us with the necessary services to work with IBeacon protocol. To receive location updates, we need to conform to the
CLLocationManagerDelegate protocol. Let us start with a short source code sample:
Before starting scanning for our beacon let’s assign our delegate object to the delegate property of our manager. It’s done in 9th line.
In the 10th line we ask the user for permission to use location services whenever the app is running. You also have the option to request location only when the app is in use. If the user allows for continual access to location, however, we can additionally listen for beacons signal when the app is in background mode — this is what we use to send notifications to The Sandwich Guy when he enters the range of a beacon.
We also added our BeaconRegion. It’s an object which defines a region used to detect the presence of IBeacon devices and it consists of 4 elements:
- UUID - used to identify beacons which are part of your application, from all other beacons in the entire world. This UUID is obviously dependent on your current device, so you’ll need to add it to the code.
- Major and Minor - parameters which identify beacons, so e.g. Major can describe floor on which this beacon is located and Minor position of this beacon on a given floor.
- Identifier - just a string for assigning your region.
These parameters must strictly specify our beacons, because each beacon must be unique. In our app it’s not a problem because we only have one beacon so we can even remove Major and Minor parameters.
So if you run your app right now, you should see this pop up:
Let’s manage this pop up options.
So when the user selects “Always allow” option, we will start searching for signals from our beacon region.
All we have to do now is to implement methods which will be triggered when we enter or leave beacon range.
And that’s all! For your convenience, I’ve put together a very simple, minimal app with basic beacon support — ready for you to extend! Find it here: https://github.com/jit-team/BeaconSample
Now you can do whatever you want when the user enters or leaves your region. In our JitEat app we simply enable and disable button with which The Sandwich Guy can send a message to our internal communicator. This way, all hungry employees always know when they can leave their rooms to meet their favourite Sandwich Guy. He will be waiting for them in the corridor with a big smile on his face because he won’t have to cover the infinite distance required to inform everybody that he is already in our office.
If you wish to see a video, which shows the application in action, visit our facebook page: