How to log the time when the user is in range ibeacon

262 Views Asked by At

I am developing an ibeacon app on swift and i would like to log the minutes that every client/user spends within a beacon range. I would really appreciate any suggestions.

1

There are 1 best solutions below

1
On

Typically you want to log this information to a server, so you can see all the times from different users in once place. That means making a web service call each time a beacon appears or disappears. Then you can calculate on the server how long it was between the time the beacon appeared and the beacon disappeared.

A typical implementation would send the following fields to the server:

event ("appear" or "disappear")
uuid
major 
minor
device_id (you can generate a new unique identifier on app install)

When do you make your web service calls? There are two answers. A simple one and a complex one:

Complex Answer

What complicates this answer is the fact that iOS apps typically track beacons using a wildcard CLBeaconRegion that leaves some of the identifiers nil. When monitoring for beacons with such a region, you don't actually know which beacons appear and disappear -- you just know when one of a group of beacons appears, and when all beacons in that same group disappear.

In order to track the individual beacons with all their identifiers, you have to use ranging APIs, which give you an update each second that a beacon is visible. But ranging only works in the foreground (and in the background for 5 seconds after the first beacon in the region appears or all disappear), so when your app goes to the background, it loses access ti fine-grained information about exactly which beacons are visible.

It is possible to build logic that combines the two techniques (ranging and monitoring) so you use ranging to track start times and monitoring to track stop times -- but in cases where you have many beacons, it only gives you a rough idea of the time each individual beacon disappeared.

Simple Answer

If you have fewer than 20 beacons to track, then this gets much simpler, because you can define a single CLBeaconRegion for each beacon, and monitor them all separately. (iOS only lets you define 20 CLBeaconRegions per app.) Then you can simply send appear/disappear events to your server for each one. With this technique, you put your web service calls in the didEnterRegion and didExitRegion callbacks.